[英]Socket exceptions in node.js
I keep getting two exceptions on my server that close the process even though the entire script is wrapped in a try catch block. 尽管整个脚本都包装在try catch块中,但我仍然在服务器上遇到两个异常,它们关闭了进程。 The exceptions are:
例外是:
events.js:160
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at exports._errnoException (util.js:1022:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:804:14)
and 和
Error: invalid opcode: 7
at Receiver.start (/home/mysite/public_html/node_modules/ws/lib/Receiver.js:211:18)
at Receiver.add (/home/mysite/public_html/node_modules/ws/lib/Receiver.js:134:14)
at Socket._ultron.on (/home/mysite/public_html/node_modules/ws/lib/WebSocket.js:139:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
From the sockets lib. 从套接字库。
Does anyone know what is causing this and how to prevent it? 有谁知道这是什么原因以及如何预防它?
edit: 编辑:
An excerpt: 摘录:
try
{
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function (client) {
if (client.readyState == WebSocket.OPEN) {
client.send(data);
}
});
};
wss.on("connection", function (ws)
{
if(blacklist.indexOf(GetClientIP(ws)) != -1)
{
console.log("Kicking banned");
ws.close();
return;
}
clientCount++;
wss.broadcast(welcomeMessage);
ws.on("message", function (message)
{
ProcessMessage(message);
});
ws.on("close", function ()
{
clientCount--;
wss.broadcast(goodbyeMessage);
});
});
}
catch(exxx)
{
console.log("Caught a exception);
console.log(exxx);
}
Attach an error handler to your websocket to catch errors: 将错误处理程序附加到您的websocket以捕获错误:
wss.on("connection", function (ws) {
...
ws.on('error', function(err) {
console.log('Websocket error!: ' + err);
});
...
});
This prevents Node.js from crashing when you get an error like EPIPE. 这样可以防止在遇到类似EPIPE的错误时Node.js崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.