ios上的套接字IO不会过去握手

时间:2017-01-19 10:55:37

标签: ios objective-c socket.io

这是我的服务器代码

var srv = https.createServer(credentials, app);
var ioRoot = socket.listen(srv);
var ioDev = ioRoot.of('/dev');
srv.listen(5014);
ioRoot.set('transports', ['polling'])
ioRoot.use(function(socket, next){
    console.log("CONSOLE.LOG SOCKET.HANDSHAKE", socket.handshake);
    next(true);
})
ioRoot.on('connection', function(ss){

    console.log('ioRoot')
    ss.emit('connect', {
        success: true
    })
})
ioDev.on('connection', function(ss){
    console.log('ioDev')
    ss.emit('connect', {
        success: true
    })
})

这是我的目标-c代码

NSURL* url = [[NSURL alloc] initWithString:@"MY URL"];
    SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"secure": @YES, @"connectParams":@{@"testing":@"YEEEEE"}}];

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

    [socket on:@"testRoot" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot");
    }];

    [socket on:@"testNsp" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot");
    }];

    [socket on:@"disconnect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot dc");
    }];
    [socket on:@"error" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot err");
    }];
    [socket on:@"reconnect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot rc");
    }];
    [socket on:@"reconnectAttempt" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot rc at");
    }];
    [socket on:@"open" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot open");
    }];
    [socket on:@"message" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"testRoot message");
    }];
    [socket onAny:^(SocketAnyEvent* event){
        NSLog(@"!!! GOT AN EVENT %@",event);
    }];

    [socket connect];

这是NodeJS LOG

engine intercepting request for path "/socket.io/" +26s
  engine handling "GET" http request "/socket.io/?transport=polling&b64=1&testing=YEEEEE" +0ms
  engine handshaking client "2RnduWALfSGN20C7AAAA" +3ms
  engine:socket sending packet "open" ({"sid":"2RnduWALfSGN20C7AAAA","upgrades":[],"pingInterval":25000,"pingTimeout":60000}) +0ms
  engine:polling setting request +1ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "86:0{"sid":"2RnduWALfSGN20C7AAAA","upgrades":[],"pingInterval":25000,"pingTimeout":60000}" +1ms
  engine:socket executing batch send callback +3ms
  socket.io:server incoming connection with id 2RnduWALfSGN20C7AAAA +0ms
  socket.io:client connecting to namespace / +0ms
  socket.io:namespace adding socket to nsp / +0ms
CONSOLE.LOG SOCKET.HANDSHAKE { headers:
   { host: MY URL,
     accept: '*/*',
     'accept-language': 'en-us',
     connection: 'keep-alive',
     'accept-encoding': 'gzip, deflate',
     'user-agent': 'TestDEV/1 CFNetwork/808.2.16 Darwin/16.3.0' },
  time: 'Thu Jan 19 2017 05:45:25 GMT-0500 (EST)',
  address: '::ffff:5.12.16.243',
  xdomain: false,
  secure: true,
  issued: 1484822725149,
  url: '/socket.io/?transport=polling&b64=1&testing=YEEEEE',
  query: { transport: 'polling', b64: '1', testing: 'YEEEEE' } }
  socket.io:client writing packet {"type":4,"nsp":"/"} +6ms
  socket.io-parser encoding packet {"type":4,"nsp":"/"} +0ms
  socket.io-parser encoded {"type":4,"nsp":"/"} as 4 +0ms
  engine:socket sending packet "message" (4) +0ms
  engine:polling closing +1m
  engine:polling transport not writable - buffering orderly close +0ms
  socket.io:client client close with reason ping timeout +0ms

这是ios日志

2017-01-19 12:45:24.452 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: connect
2017-01-19 12:45:24.452 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: testRoot
2017-01-19 12:45:24.453 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: testNsp
2017-01-19 12:45:24.453 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: disconnect
2017-01-19 12:45:24.453 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: error
2017-01-19 12:45:24.453 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: reconnect
2017-01-19 12:45:24.454 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: reconnectAttempt
2017-01-19 12:45:24.454 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: open
2017-01-19 12:45:24.454 TestDEV[42846:3759845] LOG SocketIOClient: Adding handler for event: message
2017-01-19 12:45:24.454 TestDEV[42846:3759845] LOG SocketIOClient: Adding engine
2017-01-19 12:45:24.456 TestDEV[42846:3759845] LOG SocketEngine: Starting engine. Server: MY URL
2017-01-19 12:45:24.456 TestDEV[42846:3759845] LOG SocketEngine: Handshaking
2017-01-19 12:45:24.457 TestDEV[42846:3759845] LOG SocketEnginePolling: Doing polling request
2017-01-19 12:45:24.457 TestDEV[42846:3759845] LOG SocketIOClient: Client is being released
2017-01-19 12:45:25.188 TestDEV[42846:3759845] LOG SocketEnginePolling: Got polling response
2017-01-19 12:45:25.189 TestDEV[42846:3759918] LOG SocketEngine: Got message: 0{"sid":"2RnduWALfSGN20C7AAAA","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
2017-01-19 12:45:25.190 TestDEV[42846:3759969] LOG SocketEngine: Writing poll:  has data: false
2017-01-19 12:45:25.190 TestDEV[42846:3759969] LOG SocketEnginePolling: Sending poll:  as type: 2
2017-01-19 12:45:25.191 TestDEV[42846:3759969] LOG SocketEnginePolling: Created POST string: 1:2
2017-01-19 12:45:25.191 TestDEV[42846:3759969] LOG SocketEnginePolling: POSTing
2017-01-19 12:45:25.192 TestDEV[42846:3759969] LOG SocketEngine: Engine is being released

因此,据我所知,节点js正在发送消息并期待一些响应,并且ios没有响应,因此节点js因超时而关闭客户端。 但是在ios Log上我没有看到任何相关内容。

1 个答案:

答案 0 :(得分:0)

要写这个作为答案,但只是推测:

我认为你的套接字是dealloc:

在.m文件的顶部添加

@interface CLASS_NAME ()

@property (strong, nonatomic) SocketIOClient *socket;

@end

然后改变:

SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"secure": @YES, @"connectParams":@{@"testing":@"YEEEEE"}}];

到:

self.socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"secure": @YES, @"connectParams":@{@"testing":@"YEEEEE"}}];