无法创建xmpp muc room:代码503(服务不可用)

我创建房间的代码:

XMPPRoomCoreDataStorage *rosterstorage = [[XMPPRoomCoreDataStorage alloc] init]; XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:rosterstorage jid:[XMPPJID jidWithString:@"groupchat@xmpp.getkismet.com/groupchat"] dispatchQueue:dispatch_get_main_queue()]; [xmppRoom activate:[[self appDelegate] xmppStream]]; if ([xmppRoom preJoinWithNickname:@"nameToCreateRoom"]) { NSLog(@"room created"); [xmppRoom joinRoomUsingNickname:self.userName history:nil]; } [xmppRoom fetchConfigurationForm]; [xmppRoom configureRoomUsingOptions:nil]; [xmppRoom addDelegate:[self appDelegate] delegateQueue:dispatch_get_main_queue()]; 

调试:

 2012-08-03 07:46:29.204 iPhoneXMPP[9887:fb03] room created 2012-08-03 07:46:29:230 iPhoneXMPP[9887:15003] SEND:  2012-08-03 07:46:29:237 iPhoneXMPP[9887:15003] SEND:  2012-08-03 07:46:29:326 iPhoneXMPP[9887:14f03] SEND: 91217a961321f8f6380ea2feefd0632353ad296c 2012-08-03 07:46:29:327 iPhoneXMPP[9887:14f03] RECV:  2012-08-03 07:46:29:343 iPhoneXMPP[9887:fb03] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ: 2012-08-03 07:46:29:421 iPhoneXMPP[9887:15003] RECV:  2012-08-03 07:46:29:440 iPhoneXMPP[9887:fb03] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ: 

我看到它正在创建/加入groupchat@xmpp.getkismet.com而不是像我指定的groupchat@xmpp.getkismet.com/groupchat。 我读到这很可能是问题所在。 但是,我已经指定了完整的jid,所以我迷路了。

在此先感谢所有帮助的人。

首先,看看XEP-0045:多用户聊天 。
如您所见,首先您必须发现您的用户(XMPPJID)在Jabber服务器上具有哪些function。

为此,请将下一个命令发送到Jabber服务器:

    

或使用XMPP库函数在objective-c中写入:

 NSError *error = nil; NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"" error:&error]; XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:[XMPPJID jidWithString:@"jabber.server.com"] elementID:[xmppStream generateUUID] child:query]; [xmppStream sendElement:iq]; 

现在在XMPPStream委托中侦听来自服务器的响应- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq和服务器响应应该是这样的:

       

或目标c:

 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq { if([iq isResultIQ]) { if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"]) { NSLog(@"Jabber Server's Capabilities: %@", [iq XMLString]); } } } 

现在,对于返回的每个项目,将IQ发送到您的服务器以获取它的属性,并确定哪个是会议类型,如下所示:

    

或在目标c:

 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq { if([iq isResultIQ]) { if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"]) { NSXMLElement *query = [iq childElement]; NSArray *items = [query children]; for(NSXMLElement *item in items) { NSError *error = nil; NSXMLElement *sendQuery = [[NSXMLElement alloc] initWithXMLString:@"" error:&error]; XMPPIQ *sendIQ = [XMPPIQ iqWithType:@"get" to:[XMPPJID jidWithString:[item attributeStringValueForName:@"jid"]] elementID:[xmppStream generateUUID] child:sendQuery]; [xmppStream sendElement:sendIQ]; } } } } 

听取服务器的回复:

       

并从category:conference识别群组聊天域名

 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq { if([iq isResultIQ]) { if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"]) { ... } else if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#info"]) { NSXMLElement *query = [iq childElement]; NSXMLElement *identity = [query elementForName:@"identity"]; if([[identity attributeStringValueForName:@"category"] isEqualToString:@"conference"]) { groupChatDomain = [iq fromStr]; } } } } 

最后,当我们获得群聊字段时,我们可以创建如下的聊天室:

 XMPPJID *chatRoomJID = [XMPPJID jidWithUser:@"chat_room" domain:groupChatDomain resource:@"user"]; XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init]; XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:roomChatJID dispatchQueue:dispatch_get_main_queue()]; [xmppRoom activate:xmppStream]; [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()]; [xmppRoom joinRoomUsingNickname:user history:nil]; 

并在视图控制器及其委托中添加协议:

 - (void)xmppRoomDidCreate:(XMPPRoom *)sender - (void)xmppRoomDidDestroy:(XMPPRoom *)sender - (void)xmppRoom:(XMPPRoom *)sender didConfigure:(XMPPIQ *)iqResult - (void)xmppRoom:(XMPPRoom *)sender didNotConfigure:(XMPPIQ *)iqResult - (void)xmppRoomDidJoin:(XMPPRoom *)sender - (void)xmppRoomDidLeave:(XMPPRoom *)sender - (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID withPresence:(XMPPPresence *)presence - (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID withPresence:(XMPPPresence *)presence - (void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID 

注意:在邀请其他用户访问聊天室之前,您必须发送并确认房间配置(可以邀请其他用户,但无法发送消息)。
所以你可以在创建Room之后(委托- (void)xmppRoomDidCreate:(XMPPRoom *)sender被调用)或你的用户加入(委托- (void)xmppRoomDidJoin:(XMPPRoom *)sender被调用)到聊天室。

要发送和确认房间配置,请执行以下操作之一:

 - (void)xmppRoomDidCreate:(XMPPRoom *)sender { [sender configureRoomUsingOptions:nil]; } 

要么

 - (void)xmppRoomDidJoin:(XMPPRoom *)sender { [sender configureRoomUsingOptions:nil]; } 

发送nil以接受默认选项,或者您可以将具有以下语法的IQ发送到您的服务器:

     http://jabber.org/protocol/muc#roomconfig   My Chat Room  . . .    

或客观的c代码:

 NSError *error = nil; NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"" error:&error]; NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"]; [x addAttributeWithName:@"type" stringValue:@"submit"]; NSXMLElement *field1 = [NSXMLElement elementWithName:@"field"]; [field1 addAttributeWithName:@"var" stringValue:@"FORM_TYPE"]; NSXMLElement *value1 = [NSXMLElement elementWithName:@"value" stringValue:@"http://jabber.org/protocol/muc#roomconfig"]; [field1 addChild:value1]; NSXMLElement *field2 = [NSXMLElement elementWithName:@"field"]; [field2 addAttributeWithName:@"var" stringValue:@"muc#roomconfig_roomname"]; NSXMLElement *value2 = [NSXMLElement elementWithName:@"value" stringValue:@"My Chat Room"]; [field2 addChild:value2]; //Add other fields you need, just like field1 and field2 [x addChild:field1]; [x addChild:field2]; [query addChild:x]; NSXMLElement *roomOptions = [NSXMLElement elementWithName:@"iq"]; [roomOptions addAttributeWithName:@"type" stringValue:@"set"]; [roomOptions addAttributeWithName:@"id" stringValue:[xmppStream generateUUID]; [roomOptions addAttributeWithName:@"to" stringValue:@"chat_room@conference.jabber.server.com"]; [roomOptions addChild:query]; [sender configureRoomUsingOptions:roomOptions]; 

和所有可能的配置表单字段的列表在这里

Interesting Posts