Unable to send or receive messages in Multi User Chat - XMPPFramework

三世轮回 提交于 2019-12-11 09:48:50

问题


I have created a group on Openfire server and two users. I'm able to authenticate both users using XMPPFramework but unable to exchange the messages between them. I can see both users join the group on Openfire server.

Using the following server details:

#define kHostName @"Some Server URL"
#define kServerName @"Some Server Name"
#define kRoomAddress @"testchatroom@conference.Some Server Name"    

Using the following code for stream setup and creating room:

- (void)setupStream {

    NSString *jabberID = [UserDefaults stringForKey:kUserID];
    NSString *myPassword = [UserDefaults stringForKey:kUserPassword];

    [DDLog addLogger:[DDTTYLogger sharedInstance]];

    self.xmppStream = [[XMPPStream alloc] init];
    [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    self.xmppStream.hostName = kHostName;
    self.xmppStream.hostPort = OptionPort;

    NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
    self.xmppStream.myJID = [XMPPJID jidWithString:userName];
    password = myPassword;

    NSError *error = nil;
    if (OptionOldSchoolSSL)
        [self.xmppStream oldSchoolSecureConnectWithTimeout:timeOut error:&error];
    else
        [self.xmppStream connectWithTimeout:timeOut error:&error];
}

#pragma mark -
#pragma mark XMPP delegates

- (void)xmppStreamDidConnect:(XMPPStream *)sender {

    isOpen = YES;

    Class authClass = nil;
    if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodPlain])
        authClass = [XMPPPlainAuthentication class];
    else if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodDigestMD5])
        authClass = [XMPPDigestMD5Authentication class];
    else {
        NSLog(@"Unrecognized auhthentication method '%@', falling back on Plain",
        OptionAuthenticationMethod);
        authClass = [XMPPPlainAuthentication class];
    }
    id<XMPPSASLAuthentication> auth = [[authClass alloc] initWithStream:sender
                                                               password:password];
    NSError *error = nil;
    if (![sender authenticate:auth error:&error])
        NSLog(@"Error authenticating: %@", error);
    else
        NSLog(@"Authenticated !!");
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
    [self createRoom];
}

- (void)createRoom {

    self.roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    XMPPJID *roomJID = [XMPPJID jidWithString:kRoomAddress];
    self.xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.roomStorage
                                                      jid:roomJID
                                            dispatchQueue:dispatch_get_main_queue()];

    [self.xmppRoom activate:self.xmppStream];
    [self.xmppRoom addDelegate:self
                 delegateQueue:dispatch_get_main_queue()];

    [self.xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
                                 history:nil
                                password:nil];
}

#pragma mark XMPPRoom delegates

- (void)xmppRoomDidCreate:(XMPPRoom *)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);

    NSString *jabberID = [UserDefaults stringForKey:kUserID];
    NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
    XMPPJID *jid = [XMPPJID jidWithString:userName];

    [sender inviteUser:jid withMessage:nil];
}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
{
    NSXMLElement *newConfig = [configForm copy];
    NSArray *fields = [newConfig elementsForName:@"field"];

    for (NSXMLElement *field in fields)
    {
        NSString *var = [field attributeStringValueForName:@"var"];
        // Make Room Persistent
        if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
        }
    }
    [sender configureRoomUsingOptions:newConfig];
}

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

    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];

    NSString *msg = [[message elementForName:@"body"] stringValue];
    NSString *from = [[message attributeForName:@"from"] stringValue];

    if (msg != nil && from != nil) {
        [m setObject:msg forKey:@"msg"];
        [m setObject:from forKey:@"sender"];
        [_messageDelegate newMessageReceived:m];
    }
}

Using the following code to send messages:

if ([messageStr length] > 0) {
    NSString *jabberID = [UserDefaults stringForKey:kUserID];
    NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];

    XMPPMessage * message = [[XMPPMessage  alloc] init];
    [message addAttributeWithName:@"body" stringValue:messageStr];
    [message addAttributeWithName:@"sender" stringValue:userName];

    [DELEGATE.xmppClient.xmppRoom sendMessage:message];

    self.txtMessageField.text = @"";

    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:[messageStr substituteEmoticons] forKey:@"msg"];
    [m setObject:@"you" forKey:@"sender"];
    [m setObject:[NSString getCurrentTime] forKey:@"time"];

    [self.messages addObject:m];
    [self.messagesTableView reloadData];
}

回答1:


The way how you build the XMPPMessage isn't the correctly way. The body of xmpp message is an element not an attribute.

Try this:

XMPPMessage *xmppMessage = [[XMPPMessage alloc] initWithType:@"groupchat" to:[XMPPJID jidWithString:@"user@server.com"]];
[xmppMessage addBody:@"Hi there"];

And if you want to add extra parameters:

XMPPElement *extraElement = [XMPPElement elementWithName:@"sender" stringValue:@"you"];
[xmppMessage addChild:extraElement];

And then just send:

[xmppRoom sendMessage:message];

I recommend you to implement didFailToSendMessage delegate, and check if you are receiving an error after sending your message:

- (void)xmppStream:(XMPPStream *)sender didFailToSendMessage:(XMPPMessage *)message error:(NSError *)error {}


来源:https://stackoverflow.com/questions/35156933/unable-to-send-or-receive-messages-in-multi-user-chat-xmppframework

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