Arun balaji
Arun balaji

Reputation: 131

Receiving web socket not working with socket.io library in objective c

I am using objective library in objective c, the following code works fine on emitting a socket to the server, but it doesn't receive the sockets emitted from the server.

The android client is working fine with the same format of code, so I don't think the error is with the naming of sockets. I am having trouble only with "socket on" function.

Objective C client

NSURL* url = [[NSURL alloc] initWithString:@"http://192.168.0.109:3000"];
   [socket on:@"connection" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"socket connected");
    // [socket emit:@"setUsername" with:typea];
    //   [socket emit:@"setUsername" with:@[@"test"]];
    // [manager emitAll:@"setUsername" withItems:@[@"tulasi"]];
    [manager emitAll:@"join_user" withItems:[SharedData sharedConstants].commonuserid];
    NSLog(@"socket connected:%@",[SharedData sharedConstants].commonuserid);
}];

    //    NSArray * idarray =[NSString stringWithFormat:@"@%@",[SharedData             sharedConstants].commonuserid];
      //    NSLog(@"id array is :%@",idarray);
     //
    //
  //[manager emitAll:@"join_user" withItems:@[@"commonuserid"]];



manager= [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress":            @YES}];  



socket = manager.defaultSocket;


[self->socket on:@"my_message" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"join=>%@", data.description);
    messagestr =[[data valueForKey:@"message"]objectAtIndex:0];
   // NSString * names =[[data valueForKey:@"userfrom"]objectAtIndex:0];

    NSLog(@"receive response is:%@",messagestr);

    [chatarray addObject:messagestr];

    [typearr addObject:@"b"];
    NSLog(@"left array is:%@",chatarray);
    NSLog(@"type array is:%@",typearr);



}];



[socket on:@"my_message" callback:^(NSArray * _Nonnull arrayData, SocketAckEmitter * _Nonnull ack) {

    //NSLog(@"test:%@", [arrayData description]);
    //         [self updateTableView];
     //double cur = [[arrayData objectAtIndex:0] floatValue];
    messagestr =[[arrayData valueForKey:@"message"]objectAtIndex:0];
  //  NSString * names =[[arrayData valueForKey:@"userfrom"]objectAtIndex:0];

    NSLog(@"receive response message is:%@",messagestr);
   // NSLog(@"receive response name is :%@",names);


    [chatarray addObject:messagestr];

    [typearr addObject:@"b"];
    NSLog(@"left array is:%@",chatarray);
    NSLog(@"type array is:%@",typearr);

    //message

    //typing

    [self updateTableView];

}];
NSLog(@"didReceiveEvent()");


[self->socket connect];

Upvotes: 0

Views: 688

Answers (1)

ChuckT
ChuckT

Reputation: 46

Assuming you created a SocketIOClient object named socket -

NSString *socketPath = @"http://192.168.0.109:3000";

NSURL* url = [[NSURL alloc] initWithString:socketPath];

if (! _socket) {
     NSLog(@"Creating new socket");
     _socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @NO, @"forcePolling": @YES}];
} else {
    NSLog(@"Using existing socket");
}

// Socket events
[_socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"socket connected");
}];

[_socket on:@"disconnect" callback:^(NSArray* data, SocketAckEmitter* ack) {
    _socket = nil;
    NSLog(@"socket killed");
}];

[_socket on:@"error" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"ERROR with socket %@", data);
}];

[_socket on:@"some message" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"Some message arrived with data: %@", data);
}];

Check your listening with on "connect" not "connection."

If that's not it, maybe post your error message. Also, your NSLog(@"didReceiveEvent()"); is outside the block, so it will always be called.

Upvotes: 1

Related Questions