首先,看这里XEP-0045: Multi-User Chat。
如您所见,首先您必须发现您的用户 (XMPPJID) 在 Jabber 服务器上具有哪些功能。
为此,将下一个命令发送到您的 Jabber 服务器:
<iq from='user@jabber.server.com/resource' id='some_expression' to='jabber.server.com' type='get'>
<query xmlns='http://jabber.org/protocol/disco#items'/>
</iq>
或使用 XMPP 库函数在 Objective-C 中编写:
NSError *error = nil;
NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/disco#items'/>"
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
,服务器响应应该是这样的:
<iq from='jabber.server.com' id='some_expression' to='user@jabber.server.com/resource' type='result'>
<query xmlns='http://jabber.org/protocol/disco#items'>
<item jid='im.jabber.server.com' name='Instant Message Service'/>
<item jid='conference.jabber.server.com' name='Chatroom Service'/>
</query>
</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 发送到您的服务器以获得它的属性,并确定哪一个是Conference类型,如下所示:
<iq from='user@jabber.server.com/resource' id='some_expression' to='conference.jabber.server.com' type='get'>
<query xmlns='http://jabber.org/protocol/disco#info'/>
</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:@"<query xmlns='http://jabber.org/protocol/disco#info'/>"
error:&error];
XMPPIQ *sendIQ = [XMPPIQ iqWithType:@"get"
to:[XMPPJID jidWithString:[item attributeStringValueForName:@"jid"]]
elementID:[xmppStream generateUUID]
child:sendQuery];
[xmppStream sendElement:sendIQ];
}
}
}
}
监听来自服务器的响应:
<iq from='conference.jabber.server.com' id='some_expression' to='user@jabber.server.com/resource' type='result'>
<query xmlns='http://jabber.org/protocol/disco#info'>
<identity category='conference' name='Server Group Chat Service' type='text'/>
<feature var='http://jabber.org/protocol/muc'/>
</query>
</iq>
并从身份中获取群聊域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];
<XMPPRoomDelegate>
并在您的视图控制器及其委托中添加协议:
- (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 发送到您的服务器:
<iq type='set' from='user@jabber.server.com/resource' id='some_expression' to='chat_room@conference.jabber.server.com'>
<query xmlns='http://jabber.org/protocol/muc#owner'>
<x xmlns='jabber:x:data' type='submit'>
<field var='FORM_TYPE'>
<value>http://jabber.org/protocol/muc#roomconfig</value>
</field>
<field var='muc#roomconfig_roomname'>
<value>My Chat Room</value>
</field>
.
.
.
<x>
</query>
</iq>
或目标c代码:
NSError *error = nil;
NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/muc#owner'/>"
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];
并且所有可能的配置表单字段的列表都在这里