1

我将 socket.IO-objc 库(https://github.com/pkyeck/socket.IO-objc)与运行 socket.io 的节点服务器结合使用,虽然我能够让我的 iOS 客户端连接对于服务器,我似乎无法使用objective-c api触发服务器上的消息事件。这是我的 obj-c 代码:

     - (void)viewDidLoad
    {
       [super viewDidLoad];

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

和我的服务器代码:

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

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

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

});

谁能解释为什么服务器没有收到消息事件?谢谢!

4

1 回答 1

0

wait for the

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

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

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

this should work.

于 2014-01-23T10:37:09.490 回答