How to handle MUC chat messages- messages duplicating

不羁岁月 提交于 2019-12-03 21:39:16

I think i found out an answer for the message duplication issue. The main issue was the message i am sending in the room was repeated on each room joining. What i did was when ever i sent a message i kept a deviceID as child element for the xmppmessage. On recieving on the message, i check the child element. If the devce id matches, its the same message i send earlier and its already in the core Data, So discard the message.

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

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


//device id is used, so that the my message element will be unique 
NSString *uuidString=[UIDevice currentDevice].identifierForVendor.UUIDString;
NSXMLElement *myMsgLogic=[NSXMLElement elementWithName:@"myMsgLogic" stringValue:uuidString];
[message addChild:myMsgLogic];

[self sendMessage:message];
}

Then on message recieving in xmppstream. handle it In XMPPRoomCoreDataStorage, there is a method called

 - (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room

on this do the message sorting logic. Not pasting the entire code:

- (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
{
  XMPPLogTrace();

XMPPJID *myRoomJID = room.myRoomJID;
XMPPJID *messageJID = [message from];


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

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

if ([uuidString isEqualToString:messageLogic]) {
    return;
}

 //rest code is already there in the method
 }
Siten

After searching and trying a lot i have a conclusion for receving duplicate messages for group. XMPP works best but actual issue was:

[room activate:[self appDelegate].xmppStream];

When ever we active that room it actually adds a listener. So Active only once.

Another way to do that is creating a NSPredicate:

Retrieve your echo messages (this means that you has sent a message within the room, and the XMPP server has received that message and re-sent to all (broadcast) occupants of the room with a bare like nameRoom@muc.server.com)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (messageStr CONTAINS[cd] %@)", [NSString stringWithFormat:@"from=\"%@\"",[xmpp sharedInstance].xmppStream.myJID.bare]];

If you want to show the messages that has been sent by you ( yourClientId@server.com) then change the string from by to

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (messageStr CONTAINS[cd] %@)", [NSString stringWithFormat:@"to=\"%@\"",[xmpp sharedInstance].xmppStream.myJID.bare]];

So, no need to modify the framework.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!