简体   繁体   中英

pkyeck socket.IO-objc not sending event to socket.io server

I am using the socket.IO-objc library ( https://github.com/pkyeck/socket.IO-objc ) in combination with a node server running socket.io, and while I am able to get my iOS client to connect to the server, I can't seem to trigger the message event on the server using the objective-c api. Here is my obj-c code:

     - (void)viewDidLoad
    {
       [super viewDidLoad];

       SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self];
       [socketIO connectToHost:@"my_ip_address" onPort:8080];
       [socketIO sendEvent:@"message" withData:@"hello"];

and my server code:

io.sockets.on('connection', function (socket) {

    console.log("connect to socket!");

  socket.on('message', function (data) {
    console.log("got some data!");
  });

});

can anyone explain why the server is not receiving the message event? Thanks!

wait for the

- (void) socketIODidConnect:(SocketIO *)socket
{
    NSLog(@"socket.io connected.");
}

delegate to be called and add your sendEvent: code there. eg:

- (void) socketIODidConnect:(SocketIO *)socket
{
    NSLog(@"socket.io connected.");
    [socketIO sendEvent:@"message" withData:@"hello"];
}

this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM