接受聊天室邀请
我可以使用XMPPFramework创建一个MUC,并使用下面的代码发送用户邀请请求加入该房间。
// Creating AppDelegate *dele =(AppDelegate *) [[UIApplication sharedApplication]delegate]; xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:dele jid:[XMPPJID jidWithString:self.roomName] dispatchQueue:dispatch_get_main_queue()]; [xmppRoom addDelegate:dele delegateQueue:dispatch_get_main_queue()]; [xmppRoom activate:dele.xmppStream]; [xmppRoom joinRoomUsingNickname:self.myNick history:nil]; // Inviting [xmppRoom inviteUser:[XMPPJID jidWithString:@"abc@host"] withMessage:@"Come Join me"];
用户“abc”如何知道他已收到邀请,他如何通过接受或拒绝对此做出反应?
我在XMPPFramework中找不到任何直接处理聊天室邀请的课程。 我的研究表明,每当用户收到聊天室邀请时,都会调用xmmppStream的委托方法:
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
在该消息中,我检查它是否包含名为“invite”的NSXMLElement,如果它包含,则我向用户发送回调。 然后我创建聊天室,其名称与用户收到邀请的聊天室名称相同,并进入新创建的房间。 它工作正常,但安静冗长,不安静有效。 我想知道这里是否有一个XMPPFramework类可以单独处理聊天室邀请。 例如,检测,接受和拒绝房间邀请。
我提取房间名称的代码:
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { NSXMLElement * x = [message elementForName:@"x" xmlns:XMPPMUCUserNamespace]; NSXMLElement * invite = [x elementForName:@"invite"]; NSXMLElement * decline = [x elementForName:@"decline"]; NSXMLElement * directInvite = [message elementForName:@"x" xmlns:@"jabber:x:conference"]; NSString *msg = [[message elementForName:@"body"]stringValue]; NSString *from = [[[message attributeForName:@"from"]stringValue]; if (invite || directInvite) { [self createAndEnterRoom:from Message:msg]; return; } [self.delegate newMessageRecieved:msg]; }
对于房间邀请和拒绝,实现XMPPMUCDelegate
及其方法-xmppMUC:didReceiveRoomInvitation:
-xmppMUC:didReceiveRoomInvitationDecline:
和-xmppMUC:didReceiveRoomInvitationDecline:
.
要获得房间JID,请调用[message from]
;
要加入房间,请实例化XMPPRoom
并调用-joinRoomUsingNickname:history:
.
然后让你的房间委托类实现XMPPRoomDelegate
,并实现一些委托方法来处理房间中的接收消息。
看起来目前没有更自动的方式来响应邀请。
更新:委托回调现在接收房间JID作为参数,稍微澄清了语义。
- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message; - (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitationDecline:(XMPPMessage *)message;
只需添加以下代码
if ([presenceType isEqualToString:@"subscribe"]) { [_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"localhost"]]; NSLog(@"presence user wants to subscribe %@",presenceFromUser); [xmppRoster acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES]; //For reject button // [xmppRoster rejectPresenceSubscriptionRequestFrom:[tmpPresence from]]; }
在方法内部
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence ; method