10

我可以使用 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];
}
4

2 回答 2

14

对于房间邀请和拒绝,实施XMPPMUCDelegate及其方法-xmppMUC:didReceiveRoomInvitation:-xmppMUC:didReceiveRoomInvitationDecline:

要获取房间 JID,请调用[message from];

要加入房间,请实例化 anXMPPRoom并调用-joinRoomUsingNickname:history:

然后让你的房间委托类实现XMPPRoomDelegate,并实现一些委托方法来处理房间中的接收消息。

目前似乎没有更自动的方式来响应邀请。

更新:委托回调现在接收房间 JID 作为参数,稍微澄清语义。

- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitationDecline:(XMPPMessage *)message;
于 2013-02-02T19:42:46.137 回答
-1

只需添加以下代码

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
于 2014-04-10T13:05:30.877 回答