我正在使用Robbiehanson的iOS XMPPFramework。我正在尝试创建一个MUC聊天室并邀请用户加入群聊聊天室,但是它不起作用。

我正在使用以下代码:

XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"user101@conference.jabber.org/room" nickName:@"room"];
[room createOrJoinRoom];
[room sendInstantRoomConfig];
[room setInvitedUser:@"ABC@jabber.org"];
[room activate:[self xmppStream]];
[room inviteUser:jid1 withMessage:@"hello please join."];
[room sendMessage:@"HELLO"];

用户ABC@jabber.org应该会收到邀请消息,但没有任何反应。

任何帮助将不胜感激。 :)

最佳答案

在探索了各种解决方案之后,我决定在这里编译并分享我的实现:

  • 创建XMPP session 室:
    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    
    /**
     * Remember to add 'conference' in your JID like this:
     * e.g. uniqueRoomJID@conference.yourserverdomain
     */
    
    XMPPJID *roomJID = [XMPPJID jidWithString:@"chat@conference.shakespeare"];
    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];
    
    [xmppRoom activate:[self appDelegate].xmppStream];
    [xmppRoom addDelegate:self
            delegateQueue:dispatch_get_main_queue()];
    
    [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user
                            history:nil
                           password:nil];
    
  • 检查是否在此委托(delegate)中成功创建了空间:
    - (void)xmppRoomDidCreate:(XMPPRoom *)sender
    
  • 检查您是否已加入此代表的 session 室:
    - (void)xmppRoomDidJoin:(XMPPRoom *)sender
    
  • 创建房间后,获取房间配置表:
    - (void)xmppRoomDidJoin:(XMPPRoom *)sender {
        [sender fetchConfigurationForm];
    }
    
  • 配置您的房间
    /**
     * Necessary to prevent this message:
     * "This room is locked from entry until configuration is confirmed."
     */
    
    - (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
    {
        NSXMLElement *newConfig = [configForm copy];
        NSArray *fields = [newConfig elementsForName:@"field"];
    
        for (NSXMLElement *field in fields)
        {
            NSString *var = [field attributeStringValueForName:@"var"];
            // Make Room Persistent
            if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
                [field removeChildAtIndex:0];
                [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
            }
        }
    
        [sender configureRoomUsingOptions:newConfig];
    }
    

    参考:XEP-0045: Multi-User ChatImplement Group Chat
  • 邀请用户
    - (void)xmppRoomDidJoin:(XMPPRoom *)sender
    {
        /**
         * You can read from an array containing participants in a for-loop
         * and send multiple invites in the same way here
         */
    
        [sender inviteUser:[XMPPJID jidWithString:@"keithoys"] withMessage:@"Greetings!"];
    }
    

  • 在这里,您已经创建了XMPP多用户/群组聊天室,并邀请了一个用户。 :)

    08-26 03:00
    查看更多