Tuesday 12 June 2012

XMPP + iphone starting

http://wiki.xmpp.org/web/Programming_XMPP_Clients  
http://xmpp.org/extensions/xep-0077.html#usecases-register 
http://xmpp.org/rfcs/rfc3920.html#streams

#import "NSStreamAdditions.h"
------------------------------------------
NSInputStream *inputStream;
    NSOutputStream *outputStream;
    CFReadStreamRef readStream ;
    CFWriteStreamRef writeStream;
------------------------------------------
- (void)viewDidLoad{
 [self connectToServerUsingStream:@"192.168.0.128"
                            portNo: 5222];
}

-(void) connectToServerUsingStream:(NSString *)urlStr
                            portNo: (uint) portNo {
   
    CFReadStreamRef readStream1;
    CFWriteStreamRef writeStream1;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)urlStr, portNo, &readStream1, &writeStream1);
    inputStream = (NSInputStream *)readStream1;
    outputStream = (NSOutputStream *)writeStream1;
   
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
   
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   
    [inputStream open];
    [outputStream open];
   
    NSString *sttr =  @"<iq type='get' id='reg1'>"
    "<query xmlns='jabber:iq:register'/>"
    "</iq>";
   
    NSLog(@"hello %@",sttr);
   
    NSData *data1 = [[NSData alloc] initWithData:[sttr dataUsingEncoding:NSASCIIStringEncoding]];
   
    [outputStream write:[data1 bytes] maxLength:[data1 length]];
   

}


-(void) writeToServer:(NSString*) data1 {
    if(outputStream) {
        if(![outputStream hasSpaceAvailable]) return;
        NSData *_data=[data1 dataUsingEncoding:NSUTF8StringEncoding];
        int data_len = [_data length];
        uint8_t *readBytes = (uint8_t *)[_data bytes];
        int byteIndex=0;
        unsigned int len=0;
       
        while (TRUE) {
            len = ((data_len - byteIndex >= 40960) ? 40960 : (data_len-byteIndex));
           
           
            if(len==0)
                break;
            uint8_t buf[len]; (void)memcpy(buf, readBytes, len);
            len = [outputStream write:(const uint8_t *)buf maxLength:len];
            byteIndex += len;
            readBytes += len;
        }
        NSLog(@"Sent data----------------------%@",data1);
    }

}

 ------------------------------------------

Register


NSString *data1 = @"<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'><iq type='get' xmlns='jabber:client'>"
            "<query xmlns='jabber:iq:register'/> </iq><iq type='set' xmlns='jabber:client'>"
            "<query xmlns='jabber:iq:register'>"
            "<username>piyush2</username>"
            "<password>123456</password>"
            "</query>"
            "</iq></stream:stream>";

 [self writeToServer:data1];

 login query

NSString *data1 = @"<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'><iq type='get' xmlns='jabber:client'>"
            "<query xmlns='jabber:iq:auth'>"
    "<username>piyush</username>"
    "</query> </iq>"
   
    "<iq type='set' xmlns='jabber:client' id='hariom'>"
            "<query xmlns='jabber:iq:auth'>"
                  "<username>piyush</username>"
    "<password>123123</password>"
    "<resource>iphone</resource>"
            "</query>"
            "</iq>";

 [self writeToServer:data1];

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. HI Gaurav, I am Deepak from Jaipur . I am currently working on a project which is using XMPP chat. I ahve successfully implemented the XMPP chat in the App, But I am stuck in the Registration part. I used the code snippet used by you but still I am not able to do the Registration successful.

    Hope you find some solution.
    Thanks in Advance.

    ReplyDelete