nodejs写的server 崩溃了,这个socket错误看不明白.会报这个ECONNRESET Error,求高人帮忙看看,
发布于 12 年前 作者 catyuki 16500 次浏览 最后一次编辑是 8 年前

events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: write ECONNRESET at errnoException (net.js:770:11) at Socket._write (net.js:552:19) at Socket.write (net.js:511:15) at sendMessage (D:\DevSpace\NewLobby\Lobby.js:527:11) at broadcastMessage (D:\DevSpace\NewLobby\Lobby.js:502:4) at Object.Lobby.onPlayerOffLine (D:\DevSpace\NewLobby\Lobby.js:103:4) at Socket.pro.listener (D:\DevSpace\NewLobby\lib\net\Server.js:103:19) at Socket.EventEmitter.emit (events.js:96:17) at Socket._destroy.destroyed (net.js:358:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

4 回复

错在这一行: D:\DevSpace\NewLobby\Lobby.js:527:11

贴出来看看

@leapon Lobby.js:527:11 应该是sendBuffer.write ( sendStr, 4 ); 这句话


function sendMessage( socket_,sendCmds_ ) { var sendStr = JSON.stringify( sendCmds_, replacer );
var utf8Len = Buffer.byteLength( sendStr, ‘utf8’);
var sendBuffer = new Buffer( utf8Len + 4 ) ; sendBuffer.writeInt32LE( utf8Len, 0 ); sendBuffer.write ( sendStr, 4 );
if(socket_ != null && socket_.isConnected){ socket_.write ( sendBuffer );
log.Debug("socket "+socket_.remoteAddress +’:’+ socket_.remotePort +" send size ="+ sendBuffer.length +" sendStr= "+sendStr); }else{// log.Debug(sendCmds_.cmd+“发送失败.socket连接已经断开”); } } ’

我不是高手。加上这个试试:

socket.on('error', function (exc) {
    sys.log("ignoring exception: " + exc);
});

另外 对出错部位用 try / catch, 这样程序可以报错,避免崩溃。

非常感谢.已经这么处理了…

回到顶部