XMPPFramework - 如何创建MUC房间并邀请用户?

2023-12-20

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

我正在使用以下代码:

XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"[email protected] /cdn-cgi/l/email-protection/room" nickName:@"room"];
[room createOrJoinRoom];
[room sendInstantRoomConfig];
[room setInvitedUser:@"[email protected] /cdn-cgi/l/email-protection"];
[room activate:[self xmppStream]];    
[room inviteUser:jid1 withMessage:@"hello please join."];
[room sendMessage:@"HELLO"];

用户[电子邮件受保护] /cdn-cgi/l/email-protection应该会收到邀请消息,但什么也没发生。

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


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

  1. 创建 XMPP 房间:

    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    
    /** 
     * Remember to add 'conference' in your JID like this:
     * e.g. [email protected] /cdn-cgi/l/email-protection
     */
    
    XMPPJID *roomJID = [XMPPJID jidWithString:@"[email protected] /cdn-cgi/l/email-protection"];
    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];
    
  2. 检查该委托中的房间是否已成功创建:

    - (void)xmppRoomDidCreate:(XMPPRoom *)sender
    
  3. 检查您是否已加入该代表的房间:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender
    
  4. 创建房间后,获取房间配置表:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender {
        [sender fetchConfigurationForm];
    }
    
  5. 配置您的房间

    /**
     * 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:多用户聊天 http://xmpp.org/extensions/xep-0045.html#createroom-reserved, 实施群聊 https://stackoverflow.com/questions/19268629/xmpp-ios-chat-client-implement-group-chat

  6. 邀请用户

    - (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 多用户/群组聊天室,并邀请了一个用户。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XMPPFramework - 如何创建MUC房间并邀请用户? 的相关文章

随机推荐