我正在使用iOS聊天客户端。谁能帮我进行多用户聊天吗?

我已经实现了Robbiehanson的XMPPFramework。

有人可以让我知道如何使用该框架获取组列表并在服务器中创建组吗?

提前致谢。

最佳答案

获取房间列表:

NSString* server = @"chat.shakespeare.lit"; //or whatever the server address for muc is
XMPPJID *servrJID = [XMPPJID jidWithString:server];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:servJID];
[iq addAttributeWithName:@"from" stringValue:[xmppStream myJID].full];
NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
[query addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/disco#items"];
[iq addChild:query];
[xmppStream sendElement:iq];

检查委托(delegate)方法中的响应:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
    DDLogVerbose(@"%@", [iq description]);
    return NO;
}

加入或创建房间
XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init];
NSString* roomID = @"[email protected]";
XMPPJID * roomJID = [XMPPJID jidWithString:roomID];
XMPPRoom* xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:_roomMemory
                                             jid:roomJID
                                   dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:self.xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:@"myNickname"
                        history:nil
                       password:nil];

检查XMPPRoom委托(delegate)方法中的响应:
- (void)xmppRoomDidCreate:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

更新

配置房间:

后:
[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
                        history:history
                       password:nil];

添加:
[xmppRoom fetchConfigurationForm];

并在以下位置检查响应:
- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
    DDLogVerbose(@"%@: %@ -> %@", THIS_FILE, THIS_METHOD, sender.roomJID.user);
}

查看configForm对象,并根据需要进行更改,然后将其与[sender configureRoomUsingOptions:newConfig];一起发送

示例:
要更改配置以使房间具有持久性,您可以添加以下内容:
NSXMLElement *newConfig = [configForm copy];
NSArray* fields = [newConfig elementsForName:@"field"];
for (NSXMLElement *field in fields) {
    NSString *var = [field attributeStringValueForName:@"var"];
    if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
        [field removeChildAtIndex:0];
        [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
    }
}
[sender configureRoomUsingOptions:newConfig];

(我不熟悉NSXMLElement,所以也许有更好的方法来更改值)

关于ios - XMPPFramework-实现群聊(MUC),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19268629/

10-10 00:45