XMPP MUC向房间发送消息时重复消息

时间:2016-02-29 05:32:05

标签: ios iphone xmpp multiuserchat

我正在开发聊天应用程序以实现多用户聊天功能。我能够加入房间并发送消息,但我面临一个奇怪的问题。在向房间发送消息时,消息重复/复制本身。此问题在发件人方(向房间发送消息的用户),而房间中的其他用户正在收到一条正确的消息。

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

1 个答案:

答案 0 :(得分: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];
    }
}