nodejs + socket.io iOS不传输数据

时间:2015-03-24 08:55:38

标签: ios node.js socket.io

我使用nodejs,socket.io v1.0作为我的服务器,iOS 8.2作为前端。当我尝试在我的PC上的浏览器中发送/接收内容时,一切正常,但是当我将前端切换到iOS时,它似乎似乎没有接收数据,无论是在服务器端还是在iOS端。我正在使用的库是socket.IO-objc 我的nodejs代码如下所示:

var io = require('socket.io')(3000);
io.on('connection', function (socket) {
    socket.broadcast.emit('join', socket['id']);
    console.log(socket['id'] + ' has connected!');

    socket.broadcast.emit('update', (socket['id']));

    socket.on('location', function (data) {
        console.log('location received on server');
        socket.broadcast.emit('update', (socket['id'] + ':' + data));
    });

    socket.on('disconnect', function () {
        socket.broadcast.emit('disappear', socket['id']);
        console.log(socket['id'] + ' has disconnected!');
    });
});

我的iOS代码在appDelegate.m中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    SocketIO *socketIO = [[SocketIO alloc] initWithDelegate:self];
    [socketIO connectToHost:@"192.168.0.104" onPort:3000];

    // test different event data types
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"test1" forKey:@"key1"];
    [dict setObject:@"test2" forKey:@"key2"];
    [socketIO sendEvent:@"location" withData:dict];

    return YES;
}
- (void) socketIODidConnect:(SocketIO *)socket {
    NSLog(@"====== connected ======");
}

// message delegate
- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet {
    NSLog(@"didReceiveMessage >>> data: %@", packet);
}

// event delegate
- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
    NSLog(@"didReceiveEvent >>> data: %@", packet);
}

- (void) socketIO:(SocketIO *)socket onError:(NSError *)error {
    if ([error code] == SocketIOUnauthorized) {
        NSLog(@"not authorized");
    } else {
        NSLog(@"onError() %@", error);
    }
}

- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error {
    NSLog(@"socket.io disconnected. did error occur? %@", error);
}

运行时,XCode会给我以下NSLog消息:

2015-03-24 17:14:48.202 soccer[11285:248403] Connecting to socket with URL: 
http://192.168.0.104:3000/socket.io/1/?EIO=2&transport=polling&t=1427188488203
2015-03-24 17:14:48.203 soccer[11285:248403] Prepare to send()
2015-03-24 17:14:48.203 soccer[11285:248403] queue >>> 5:::{"name":"location","args":[{"key1":"test1","key2":"test2"}]}
2015-03-24 17:14:48.237 soccer[11285:248403] didReceiveResponse() 200
2015-03-24 17:14:48.237 soccer[11285:248403] connectionDidFinishLoading()
2015-03-24 17:14:48.239 soccer[11285:248403] VERSION 10x
2015-03-24 17:14:48.240 soccer[11285:248403] Response {"sid":"pdmp_5FzH0NqVqoMAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2015-03-24 17:14:48.240 soccer[11285:248403] websocket supported -> using it now
2015-03-24 17:14:48.240 soccer[11285:248403] Received All information:
    sid: pdmp_5FzH0NqVqoMAAAA
    heartbeat: 25.000000
    Transport: <SocketIOTransportWebsocket: 0x7f98224242f0>
2015-03-24 17:14:48.241 soccer[11285:248403] start/reset Heartbeat

Nodejs给了我以下内容:

$ node app.js
pdmp_5FzH0NqVqoMAAAA has connected!

看起来套接字已连接,它只是无法接收任何数据......有人可以帮忙吗?感谢。

0 个答案:

没有答案