引用此评论
引用:LanjunLi 发表于 2023-05-25 10:02 nodejs-websocket 包在客户端关闭时会导致服务器端发生异常而退出。
let ws = require("nodejs-websocket"); let server = ws.createServer((conn) => { console.log("new connection"); conn.on("text", (msg) => { broadcast(server, msg); }); conn.on("close", () => { console.log("connection closed"); }); }); function broadcast(server, msg) { server.connections.forEach((conn) => { conn.sendText(msg); }); } server.listen(3001)
const ws = new WebSocket("ws://127.0.0.1:3001"); ws.onopen = function () { oUl.innerHTML += "<li>客户端链接成功</li>"; oSend.onclick = function () { if (!/^\s*$/.test(oText.value)) { ws.send(oText.value); } }; }; ws.onmessage = function (msg) { oUl.innerHTML += `<li>${msg.data}</li>`; }; ws.onclose = function () { oUl.innerHTML += "<li>客户端链接断开</li>"; }
connection closed Uncaught Error: read ECONNRESET __node_internal_captureLargerStackTrace __node_internal_errnoException onStreamRead callbackTrampoline Async call from TickObject init emitInitNative emitInitScript nextTick onDestroy Socket._destroy _destroy destroy onStreamRead callbackTrampoline Async call from TCPWRAP init emitInitNative
精华主题数超过100个。
连续任职达2年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.125000 second(s), 32 queries , Gzip On.