node.js - Nodejs长连接服务器iOS端报错 Error Domain=GCDAsyncSocketErrorDomain Code=7
迷茫
迷茫 2017-04-18 09:56:05
0
1
707

我用node.js 写了一个长连接服务, 当收到客服端数据后返回一条成功消息,然后用tcp keepalive 保持连接.需求要求客户端一直连上, 但目前的情况是在iOS 端连上一段时间就断开了并在iOS端收到的错误如下:

Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo={NSLocalizedDescription=Socket closed by remote peer}

服务器代码:

var net = require('net');
var netKeepAlive = require('net-keepalive');
var redis = require('redis'); 
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
    // In this case its a socket server
    net.createServer(function (sock) {

        sock.setTimeout(10*1000);
        //IMPORTANT: KeepAlive must be enabled for this to work 
        sock.setKeepAlive(true, 1000); 
        // Set TCP_KEEPINTVL for this specific socket 
        netKeepAlive.setKeepAliveInterval(sock, 1000);
        // and TCP_KEEPCNT 
        netKeepAlive.setKeepAliveProbes(sock, 1);

        sock.on('timeout',function(){
            console.log((new Date()) + 'timeout');

        });

       
        console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);

       
        // 为这个socket实例添加一个"data"事件处理函数
        sock.on('data', function (data) {
            console.log((new Date()) + ': DATA ' + sock.remoteAddress + ': ' + data );
            //测试阻塞
            // sleep(20000); 
            console.log((new Date()) + ': DATA ' + sock.remoteAddress + ': ' + data );

            sock.write('{ "msg": "成功", "code": "200" }');
        });

        // 为这个socket实例添加一个"close"事件处理函数
        sock.on('close', function (data) {
            console.log((new Date()) + ': CLOSED By: ' + sock.remoteAddress + ' ' + sock.remotePort);
        });

        // 为这个socket实例添加一个"error"事件处理函数, 结束后会触发 close
        sock.on('error', function (data) {
        });

        // 为这个socket实例添加一个"end"事件处理函数, 结束后会触发 close
        sock.on('end', function (data) {
        });

    }).listen(PORT, HOST);
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

membalas semua(1)
刘奇
sock.setTimeout(10*1000);
        //IMPORTANT: KeepAlive must be enabled for this to work 
        sock.setKeepAlive(true, 1000); 
        // Set TCP_KEEPINTVL for this specific socket 
        netKeepAlive.setKeepAliveInterval(sock, 1000);
        // and TCP_KEEPCNT 
        netKeepAlive.setKeepAliveProbes(sock, 1);

Jika masa yang ditetapkan di sini terlalu singkat, masalah ini akan berlaku, kerana jika pelayan menjadi tidak stabil sekali, ia akan terputus secara automatik.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan