接受邀请,聊天室(Accepting chatroom invitation)

2019-07-19 04:23发布

我能创造一个MUC采用XMPPFramework并发送用户邀请请求使用下面的代码加入该房间。

// 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

在该消息,我检查是否含有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];
}

Answer 1:

对于聊天室邀请和下降,实施XMPPMUCDelegate其方法-xmppMUC:didReceiveRoomInvitation:-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;


Answer 2:

只需添加下面的代码

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


文章来源: Accepting chatroom invitation