简体   繁体   中英

Xmpp MultiUserChat (MUC) Group Does not remain stable

I created an Xmpp Chat App where I have implemented the one-to-one and group chat. The Chat itself is working fine. The issue is in Group chat. I created a group with 2-3 members, again the chat is working fine, but when I kill the application and restart it, I'm not getting the group messsages from any of the groups I have created. while I am connected to the XMPP Server and re-join any group then I get the messages. My problem is that I have to join into groups again every time after I kill the app completly.

Please let me know How I can get the messages or join automatically in group when i open the application from killed state.

You need to send presence to XMPP server once your application launched or come out from background. so the XMPP server understand that respective group is ready to handle event.

Edit : you can send presence using following code.

- (void)goOnline {


    NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
    NSXMLElement *show = [NSXMLElement elementWithName:@"show"
                                           stringValue:@"dnd"];
    NSXMLElement *status = [NSXMLElement elementWithName:@"status" stringValue:@"available"];
    NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" stringValue:@"24"];

    [presence addChild:show];
    [presence addChild:status];
    [presence addChild:priority];

    [_xmppStream sendElement:presence];

    [self createOrJoinRoom];

}
- (void)createOrJoinRoom {
    if ([appDelegate.xmppStream isConnected]) {

        NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"XMPPUserId"];

        NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
        [presence addAttributeWithName:@"from" stringValue:[[appDelegate.xmppStream myJID]full]];
        [presence addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@@%@/%@", @"newone", GroupChatRoomName,myJID]];
        NSXMLElement *xelement = [NSXMLElement elementWithName:@"x" xmlns:XMPPMUCNamespace];
        [presence addChild:xelement];
        [appDelegate.xmppStream sendElement:presence];
    }

}

May this help you.

You have to join all your previous join/connected groups. Because in iOS if you kill your app then you left from your created or joined groups.

So every time in this section of the code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

you have to join your group again.

Below is demo code for it :

XMPPRoomHybridStorage *xmppRoomStorage1 = [XMPPRoomHybridStorage sharedInstance];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomStorage1 jid:RoomName];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:appDelegate.Obj_xmppManager.xmppStream];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"1"];
[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user history:nil];

Adding to the Presence answer, I would also check more basic things -

  1. Are the rooms (group chat) you create persistant? or do you have to create the room again every time you connect? (notice the difference between 'openning' and 'creating').
    On some servers, rooms are temporary by default - you can check this by connecting with 2 seperate clients, send some messages, disconnect only one of them, and reconnect - if you do see the messages that were sent in your reconnected client - this might be your issue, can you show the parameters you pass to the server when you create the room?.

  2. Is the server you are using configured to send history messages by default, and if so how many, again, server implementations may vary, could you share some information on the server you are using (openfire, ejabbered, prosody)? or a snippet from you configuration file?

  3. Is it possible you are getting the messages, but not showing them correctly, maybe not refreshing the screen\\view when first entering a room? any log messages?

I am also facing this issue since a week and looking for solution and after a lot of search on google and stack overflow i got a clue which solve this issue.

In my case group created successfully and chat is working good with members and members can send me chat too but when any member of group logout or kill the app and then login again he is unable to send message is this group and group says in response Only occupants are allowed to send messages to the conference .

In my case when user tap on group to go into group and start chat i call this method to join group.

NSString *roomJID = [NSString stringWithFormat:@"%@@conference.yourHostName", roomJid];
XMPPJID *jid = [XMPPJID jidWithString:roomJID];

_xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomCoreDataStorage jid:jid dispatchQueue:dispatch_get_main_queue()];
[_xmppRoom activate:stream];
[_xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[_xmppRoom joinRoomUsingNickname:stream.myJID.bare history:nil];

Hope this will work for you too

By default a MUCRoom will send some history to newly joined user, the number is determined by config, under mod_muc: history_size: . Or you need to explicitly request for some amount of history while sending the Presence, doc :

<presence
    from='hag66@shakespeare.lit/pda'
    id='n13mt3l'
    to='coven@chat.shakespeare.lit/thirdwitch'>
  <x xmlns='http://jabber.org/protocol/muc'>
    <history maxstanzas='20'/>
  </x>
</presence>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM