XMPP聊天未显示给其他用户结束

时间:2015-07-04 11:09:05

标签: ios objective-c xcode xmpp chat

在我的项目中实施Xmpp Chat。当我从一个(X-User)用户发送消息时,消息发送成功并显示在用户(X用户)屏幕上。但是其他(Y-User)用户屏幕上未显示消息。当其他用户发送消息时,它也没有显示在他自己的屏幕上。但它显示在(X用户)屏幕上。这意味着消息发送&删除工作正常。但是消息没有显示在另一端` - (void)sendMessage {

if(![self appDelegate].isXmppConnected)
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Not Connected" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alert show];
    return;
}

if (![[messageTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
    [CommonMethods showAlertWithTitle:@"Message" withMessage:@"Please enter text"];
    return;
}
NSString *messageStr = nil;
//        messageStr =  [[textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByEncodingHTMLEntities];
NSMutableDictionary *dict =  [[NSMutableDictionary alloc]init];
[dict setObject:[messageTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"text"];
[dict setObject:@"text" forKey:@"type"];


NSString *jsonString = [dict JSONRepresentation];

messageStr =  [jsonString stringByEncodingHTMLEntities];
messageTextView.text = @"";

//[messageTextView refreshHeight];
NSString *messageID = [[[self appDelegate] xmppStream] generateUUID];

if([messageStr length] > 0)
{
    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:messageStr];

    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:self.mJabberIDString];
    [message addAttributeWithName:@"id" stringValue:messageID];
    [message addChild:body];
    XMPPMessage * xMessage = [XMPPMessage messageFromElement:message];
    [xMessage addPausedChatState];

    XMPPElementReceipt *receipt;
    [[self appDelegate].xmppStream sendElement:xMessage andGetReceipt:&receipt];
    self.sendChatState = true;

    if ([receipt wait:-1])
    {
        NSLog(@"maraChat - sendMessage - messageSent");
       [self.chatTableView reloadData];
    }
    else
    {
        NSLog(@"maraChat - sendMessage - Failed *********** ");
        [self saveMessageDatabaseForMessageFailToSent:messageStr];
    }
}

}

以上发送消息的代码

#pragma mark- xmppstream did receive message

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSLog(@"xmpp stream did receive message");

NSString *chatType = [[message attributeForName:@"type"]stringValue];

if ([chatType isEqualToString:@"chat"] && ![[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
    NSString *toJabberId = [[message attributeForName:@"from"]stringValue];
    // NSLog(@"to jabber id is:%@",toJabberId);
    NSArray *componentArray = [toJabberId componentsSeparatedByString:@"/"];
    if (componentArray && [componentArray count] > 0) {
        toJabberId = [componentArray objectAtIndex:0];
        // NSLog(@"to jabber id is:%@",toJabberId);
        // NSLog(@"my jabber id is:%@",toJabberId);
        // NSLog(@"my jabber id is:%@",[[self.xmppStream myJID]bare]);
        if ([toJabberId hasPrefix:[[self.xmppStream myJID]bare]] || [[[self.xmppStream myJID] full] hasPrefix:toJabberId]) {
            return;
        }
    }
    NSString *msgBody = [[message elementForName:@"body"]stringValue];
    // We are not active, so use a local notification instead
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.alertAction = @"View";
    {
        if ([msgBody hasPrefix:@"This"]) {
            return;
        }
        else {
            NSString *toJabberId = [[message attributeForName:@"from"]stringValue];
            // NSLog(@"to jabber id is:%@",toJabberId);
            NSString *displayName = nil;
            NSArray *componentArray = [toJabberId componentsSeparatedByString:@"@"];
            if (componentArray && [componentArray count]) {
                displayName = [componentArray objectAtIndex:0];
            }

            NSDictionary *dict = [[msgBody stringByDecodingHTMLEntities] JSONValue];
            int isImageAttached = 0;
            if ([[dict valueForKey:@"type"] isEqualToString:@"image"]) {
                msgBody = @"Sent an Image";

            }
            else if ([[dict valueForKey:@"type"] isEqualToString:@"location"]) {
                msgBody = @"Suggest a location";
            }
            else if ([[dict valueForKey:@"type"] isEqualToString:@"video"]) {
                msgBody = @"Sent an Video";
            }
            else if ([[dict valueForKey:@"type"] isEqualToString:@"audio"]) {
                msgBody = @"Sent an Audio";
            }
            else {
                msgBody = [dict valueForKey:@"text"];
            }


            localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",displayName,msgBody];
            localNotification.soundName=@"Messagerecieved.caf";
            localNotification.fireDate = [NSDate date];
            localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
            [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
        }
    }
    return;
}

else {
    NSXMLElement * x = [message elementForName:@"x" xmlns:XMPPMUCUserNamespace];
    NSXMLElement * invite  = [x elementForName:@"invite"];
    NSString *msg = [[message elementForName:@"body"]stringValue];
    NSString *from = [[message attributeForName:@"from"]stringValue];

    if ([message hasChatState])
    {
        NSString *jId = [message fromStr] ;//[user displayName];
        NSRange range = [jId rangeOfString:@"/"];
        if (range.location != NSNotFound) {
        }

        NSArray *parts = [jId componentsSeparatedByString:@"/"];
        NSString *emailAddress;
        if ([parts count] == 2) {
            emailAddress = [parts objectAtIndex:0];   // equal to "user"
        }

        // NSXMLElement *delivered  = [message elementForName:@"delivered"];

        NSXMLElement * x = [message elementForName:@"x"];
        NSXMLElement * delivered  = [x elementForName:@"delivered"];
        NSXMLElement *displayed  = [x elementForName:@"displayed"];



        if (delivered) {
            NSLog(@"message delivered");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"User_Not_Doing_Typing" object:nil];
            return;
        }
        if (displayed) {
            NSLog(@"message seen");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"User_Not_Doing_Typing" object:nil];
            return;
        }




        if (msg == nil) {
            if ([self.mGlobalChattedJabberID isEqualToString:emailAddress]) {
                if ([message isChatMessage]) {
                }
                else {
                    if (![jId isEqualToString:[[self.xmppStream myJID] full]]) {
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"User_Doing_Typing" object:nil];
                    }

                }
            }
            else { // chat state
                if (self.mGlobalChattedJabberID != nil)
                {
                    if (![[UIApplication sharedApplication]applicationState] == UIApplicationStateActive) {
                        NSString *toJabberId = [[message attributeForName:@"from"]stringValue];
                        // NSLog(@"to jabber id is:%@",toJabberId);
                        NSString *displayName = nil;
                        NSArray *componentArray = [toJabberId componentsSeparatedByString:@"@"];
                        if (componentArray && [componentArray count]) {
                            displayName = [componentArray objectAtIndex:0];
                        }


                        NSString *msgBody = [[message elementForName:@"body"]stringValue];
                        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                        localNotification.alertAction = @"View";

                        localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",displayName,msgBody];
                        localNotification.fireDate = [NSDate date];
                        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
                        [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
                    }

                    if ([self.mGlobalChattedJabberID isEqualToString:emailAddress])
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"User_Not_Doing_Typing" object:nil];
                }

            }
            if (msg == nil)
            {
                return;
            }
        }

    }

}
}  

请帮我解决这个问题

0 个答案:

没有答案