Javascript node.js中的套接字异常
我的服务器上不断出现两个异常,它们关闭了进程,即使整个脚本都包装在一个try-catch块中。 例外情况如下:Javascript node.js中的套接字异常,javascript,node.js,sockets,websocket,Javascript,Node.js,Sockets,Websocket,我的服务器上不断出现两个异常,它们关闭了进程,即使整个脚本都包装在一个try-catch块中。 例外情况如下: 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) 及 从套接
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)
及
从套接字库
有人知道是什么导致了这种情况,以及如何预防吗
编辑:
摘录:
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);
}
将错误处理程序附加到websocket以捕获错误:
wss.on("connection", function (ws) {
...
ws.on('error', function(err) {
console.log('Websocket error!: ' + err);
});
...
});
这可以防止Node.js在出现EPIPE之类的错误时崩溃。您能显示您的代码吗?@AymanElTemsahi整个代码非常大,但这是连接代码的要点。try-catch子句中根本没有捕获异常。wss来自哪个外部模块?在我看来,您缺少一些套接字写入错误的错误处理。高级try/catch不会捕获套接字写入的异步错误。根据wss和ws来自哪个模块的文档,您必须在本地捕获这些信息。@jfriend00它来自ws库,您选择的模块似乎没有关于如何处理错误的适当文档。如果您真的想知道,您必须详细研究该模块的源代码,并弄清楚它会如何处理错误。这是我能找到的全部,我甚至不确定这是正确的模块,也不确定这是您遇到的错误类型。
wss.on("connection", function (ws) {
...
ws.on('error', function(err) {
console.log('Websocket error!: ' + err);
});
...
});