Unable to send messages using xmpp

I have developed a chat application in iOS using XMPP framework. Have successfully done the authentication of the user. I am also able to receive the messages from other user. But unable to send messages to the same. I use the XMPPStream class's sendElement method to send messages. The connection gets established successfully at the beginning, ie while login, also i'm able to view the online and away users.But the connection gets lost only while sending messages. The connection state is always shown as disconnected in the send element method ie STATE_XMPP_DISCONNECTED, whenever i try to send the message.

Can somebody let me know why my connection loss occurs while sending message to any user.

I have gone through the links below, but didn't get any help. 1)XMPPFramework message not sent 2)Send a message via XMPPFramework for iOS

Flow of my app is as follows: After login, a list of users online/away is displayed. On clicking a user, i get the next screen to chat with him, wherein I type my message and hit the send button.

Here is my code to send the message:

 - (IBAction)sendMessage {

  NSString *messageStr = self.messageField.text;
  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:[NSString stringWithFormat:@"%@@cwt-dst-w005",chatWithUser ]];
    [message addChild:body];
    self.messageField.text = @"";
    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:messageStr forKey:@"msg"];
    [m setObject:@"you" forKey:@"sender"];      
    [messages addObject:m];
    [self.tView reloadData];

    NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:messages.count-1
                                               inSection:0];
    [self.tView scrollToRowAtIndexPath:topIndexPath
                  atScrollPosition:UITableViewScrollPositionMiddle
                          animated:YES];
  }

And here is where I get the state as disconnected.

 - (void)sendElement:(NSXMLElement *)element
{
if (element == nil) return;
NSLog(@"STATE--->%d",state);
dispatch_block_t block = ^{ @autoreleasepool {

    if (state == STATE_XMPP_CONNECTED)
    {
        [self sendElement:element withTag:TAG_XMPP_WRITE_STREAM];
    }
    else
    {
        [self failToSendElement:element];
    }
}};

if (dispatch_get_specific(xmppQueueTag))
    block();
else
    dispatch_async(xmppQueue, block);
}
链接地址: http://www.djcxy.com/p/93524.html

上一篇: 将Facebook消息作为特定页面发送

下一篇: 无法使用xmpp发送消息