开发者

XMPPFramework - How to create a MUC room and invite users?

开发者 https://www.devze.com 2023-03-22 00:02 出处:网络
I am using Robbiehanson\'s iOS XMPPFramework. I am trying to create a MUC room and invite a user to the group chat room but it is not working.

I am using Robbiehanson's iOS XMPPFramework. I am trying to create a MUC room and invite a user to the group chat room but it is not working.

I am using the following code:

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"];
开发者_StackOverflow

The user ABC@jabber.org should receive the invite message but nothing is happening.

Any help will be appreciated. :)


After exploring various solutions, I've decided to compile and share my implementation here:

  1. Create an XMPP Room:

    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. Check if room is successfully created in this delegate:

    - (void)xmppRoomDidCreate:(XMPPRoom *)sender
    
  3. Check if you've joined the room in this delegate:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender
    
  4. After room is created, fetch room configuration form:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender {
        [sender fetchConfigurationForm];
    }
    
  5. Configure your room

    /**
     * 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];
    }
    

    References: XEP-0045: Multi-User Chat, Implement Group Chat

  6. Invite users

    - (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!"];
    }
    

There, you've created a XMPP multi-user/group chat room, and invited a user. :)


I have the feeling that the first thing to do after alloc-init is to attach it to your xmppStream, so it can use xmppStream to send/receive messages.

More exactly:

XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"user101@conference.jabber.org/room" nickName:@"room"];
[room activate:[self xmppStream]];

//other things (create/config/...)


Check the latest XMPPMUCLight & XMPPRoomLight its similar to Whatsapp and other today's trends social app rooms that don't get destroyed or members kicked when offline or no one in room.

Refer this documentation & mod from MongooseIM

0

精彩评论

暂无评论...
验证码 换一张
取 消