【问题标题】:XMPPFramework - How to create a MUC room and invite users?XMPPFramework - 如何创建 MUC 房间并邀请用户?
【发布时间】:2011-10-10 20:05:54
【问题描述】:

我正在使用 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 应该会收到邀请消息,但没有任何反应。

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

【问题讨论】:

    标签: ios objective-c xmpp xmppframework


    【解决方案1】:

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

    1. 创建 XMPP 房间:

      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];
      
    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: Multi-User ChatImplement 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 多用户/群聊室,并邀请了一位用户。 :)

    【讨论】:

    • @NaveedRafi 你当然是最受欢迎的。我希望这对其他 XMPP 用户也有帮助。 :-)
    • 谢谢!有没有办法设置房间密码?我想开个私人房间。
    • @rohitmandiwal 我的荣幸!如上所示,您可以通过此行创建受密码保护的 MUC 房间 - [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user history:nil password:@"myPassword"];
    • 大家好,谢谢大家和starckoverflow,我可以使用存储(核心数据和内存存储)创建组并向其他人发送邀请。问题是当我创建第二组时,它会从核心数据存储中删除第一组数据,而且我们如何自动加入其他用户?
    • @KeithOYS - 非常感谢这段代码。我无法理解用户加入房间的第 3 步。我如何知道用户是否加入了房间。此外,如果您能帮助我们了解我们在实施后如何接收和发送消息。非常感谢您的帮助。
    【解决方案2】:

    我感觉alloc-init之后要做的第一件事就是把它附加到你的xmppStream上,这样它就可以使用xmppStream来发送/接收消息了。

    更准确地说:

    XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"user101@conference.jabber.org/room" nickName:@"room"];
    [room activate:[self xmppStream]];
    
    //other things (create/config/...)
    

    【讨论】:

      【解决方案3】:

      查看最新的 XMPPMUCLight 和 XMPPRoomLight,它类似于 Whatsapp 和其他当今趋势的社交应用程序房间,它们在离线或房间内无人时不会被破坏或成员被踢。

      参考这个documentation & mod from MongooseIM

      【讨论】:

      猜你喜欢
      • 2018-10-07
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2013-09-04
      • 2015-01-14
      相关资源
      最近更新 更多