1

I am working on a chat application to achieve multiuser chat functionality. I am able to join room and send message but i am facing a strange problem. While sending message to room, message repeat/duplicate itself. This issue is in sender side (user who is sending message to room) while other users in room are getting one message which is correct.

- (void)sendMessageWithBody:(NSString )messageBody andMessageId:(NSString) messageId 
{ 
if ([messageBody length] == 0) return; 
NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:messageBody];
 XMPPMessage *message = [XMPPMessage message];
 [message addAttributeWithName:@"id" stringValue:messageId];
 [message addChild:body];
 [self sendMessage:message]; 
} 
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
Amrit
  • 301
  • 2
  • 14
  • - (void)sendMessageWithBody:(NSString *)messageBody andMessageId:(NSString*) messageId { if ([messageBody length] == 0) return; NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:messageBody]; XMPPMessage *message = [XMPPMessage message]; [message addAttributeWithName:@"id" stringValue:messageId]; [message addChild:body]; [self sendMessage:message]; } – Amrit Feb 29 '16 at 05:55
  • check this http://stackoverflow.com/questions/26681309/how-to-handle-muc-chat-messages-messages-duplicating – Badal Shah Feb 29 '16 at 06:10
  • @BadalShah i have tried above link but could not suceed. – Amrit Mar 01 '16 at 03:59
  • Got It. In XMPPMessageArchiving.h.m file under XEP-0136, this file have method - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message. So inside this method we have to write our logic so that duplicate message will not be added. – Amrit Mar 01 '16 at 04:12

1 Answers1

0
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    XMPPLogTrace();

    NSString* fromStr;
    NSArray* arrayFrom = [[message fromStr] componentsSeparatedByString:@"/"];
    if ([arrayFrom count] > 1)
    {
        fromStr = [arrayFrom objectAtIndex:1];
    }

    NSString* toStr;
    NSArray* arrayTo = [[message toStr] componentsSeparatedByString:@"@"];

    if ([arrayTo count] > 1)
    {
        toStr = [arrayTo objectAtIndex:0];
    }
    if ([fromStr isEqualToString:toStr] || ([[message body] length] == 0)) {
        return;
    }
    if ([self shouldArchiveMessage:message outgoing:NO xmppStream:sender])
    {
        [xmppMessageArchivingStorage archiveMessage:message outgoing:NO xmppStream:sender];
    }
}
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
Amrit
  • 301
  • 2
  • 14