2

I am working on the Chat concept by using XMPP framework. I am successfully able to make connection with the server. Now my next step is to enter in the given room.

        NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
        NSString *room = [@"myroom" stringByAppendingString:@"@app.xmpp.syn.in"];
        [presence addAttributeWithName:@"to" stringValue:room];
        NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"http://jabber.org/protocol/muc"];
        NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
        [history addAttributeWithName:@"maxstanzas" stringValue:@"50"];
        [x addChild:history];
        [presence addChild:x];
        XMPPIQ *iq = [XMPPIQ iq];
        [iq addChild:presence];
        [xmppStream sendElement:presence];

I have tried with the above code but it doesn't work. It doesn't go to following method.

- (void)sendElement:(NSXMLElement *)element
{
    if (element == nil) return;
    dispatch_block_t block = ^{ @autoreleasepool {
        if (state == STATE_XMPP_CONNECTED)
        {
            [self sendElement:element withTag:TAG_XMPP_WRITE_STREAM];
        }
        else
        {
            NSError *error = [NSError errorWithDomain:XMPPStreamErrorDomain
                                                 code:XMPPStreamInvalidState userInfo:nil];
            [self failToSendElement:element error:error];
        }
    }};

    if (dispatch_get_specific(xmppQueueTag))
        block();
    else
        dispatch_async(xmppQueue, block);
}

I am implementing this very first time. May be I am wrong to enter in the MUC room. Please correct or suggest me with this issue.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Sudha Tiwari
  • 2,499
  • 26
  • 50

1 Answers1

0

First of all, these lines are wrong:

    XMPPIQ *iq = [XMPPIQ iq];
    [iq addChild:presence];

The 3 stanzas XMPP uses are <presence>, <message> and <iq>. You send these as separate children of the XML <stream:stream>. Wrapping a presence in an iq makes no sense. But in your example code you're not using iq after doing this, so this probably is not what causes you problem.

Secondly, you should send your presence to a full JID, not a bare JID. A full JID is a jid that includes a resource, so myroom@app.xmpp.syn.in/resource, instead of myroom@app.xmpp.syn.in. What you specify as resource will be your nickname in the chat. See Example 18 in XEP-0045. So instead, you should do something similar to:

    NSString *room = [[@"myroom" stringByAppendingString:@"@app.xmpp.syn.in"] stringByAppendingString:@"/nickname"];
    [presence addAttributeWithName:@"to" stringValue:room];
xnyhps
  • 3,306
  • 17
  • 21