问题描述
我用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'事件处理函数, 结束后会触发 closesock.on(’error’, function (data) {});// 为这个socket实例添加一个'end'事件处理函数, 结束后会触发 closesock.on(’end’, function (data) {}); }).listen(PORT, HOST);}
问题解答
回答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);
此处设置的时间太短会产生这样的问题, 因为服务器端若有一次不稳定就主动断开了.