IOS Chat application using XMPP Protocol (ejabberd

2019-06-08 07:30发布

问题:

I am developing an IOS Chat application using XMPP Protocol(ejabberd). My chat room is created at my server, it return roomID to me.

I am facing an issue in room/group chat. When i am sending a single message it is repeating more than once like 3 to 4 times.How to fix this. My code is here

 XMPPJID *roomJID = [XMPPJID jidWithString:[roomDict objectForKey:KEY_group_id]];

XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomCoreDataStorage jid:roomJID dispatchQueue:dispatch_get_main_queue()];

[xmppRoom activate:[ChatHandler sharedInstance].xmppStream];
[xmppRoom addDelegate:self
        delegateQueue:dispatch_get_main_queue()];
[self insertRoomObjectWithDictionary:roomDict];
[xmppRoom joinRoomUsingNickname:[[ChatHandler sharedInstance] xmppStream].myJID.user
                        history:nil
                       password:@""];
[xmppRoom fetchConfigurationForm];
return xmppRoom;

回答1:

Following code snippet worked for me.. Try it for your code... I have sent uuid of my device as child while sending message and checked the same uuid at the time of incoming message :

-(void)sendMessageWithBody:(NSString *)messageBody
{
    if ([messageBody length] == 0) return;

    NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:messageBody];

    XMPPMessage *message = [XMPPMessage message];
    [message addChild:body];

    NSString *uuidString=[UIDevice currentDevice].identifierForVendor.UUIDString;

    NSXMLElement *myMsgLogic=[NSXMLElement elementWithName:@"myMsgLogic" stringValue:uuidString];
    [message addChild:myMsgLogic];

    [self sendMessage:message];
}

-(void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID;
{
    [self handleIncomingMessage:message room:xmppRoom];
}

-(void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
{
    NSString *uuidString=[UIDevice currentDevice].identifierForVendor.UUIDString;

    NSString *messageLogic= [[message elementsForName:@"myMsgLogic"].firstObject stringValue];

    if ([uuidString isEqualToString:messageLogic]) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"handleIncomingMessage" message:[message body] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
}


回答2:

I have the same chat application using xmpp ejabbered. I was also facing the same problem. In my case on xmpp server they have set offline message storage to 100. If in offline mode the message limit crossed 100 then from 101th message I will get bounce back that message. So as a solution we changed the offline message limit to 500.

{mod_offline_odbc, [
 {user_max_messages, 500}
]}