Deno ws를 사용하여 문서를 다시로드하려고하는데 두 번째 다시로드 후 오류가 발생합니다.
Uncaught ConnectionReset: Socket has already been closed throw new Deno.errors.ConnectionReset("Socket has already been closed");
var ws = new WebSocket("ws://127.0.0.1:8080/ws")
ws.onopen = function () {
ws.send('ws open')
console.log('ws open');
}
ws.addEventListener("message", (e) => {
if (e.data === 'fileUpdate') {
// ws.send('close')
location.replace(location.href);
}
})
location.replace(location.href)
어떤 해결책이 오류를 일으키는 것 같습니까?
소켓이 닫힌 후 메시지를 보내므로 오류가 발생합니다 .
할 때 : location.replace(location.href);
페이지가 새로 고쳐지고 현재 소켓이 닫힙니다.
오류를 포착하거나 ws.isClosed
메시지를 보내기 전에 확인할 수 있습니다 .
for await (const e of ws) {
if (e === 'close') {
ob.remove("fileUpdate")
continue
}
ob.on("fileUpdate", () => {
console.log('sending')
if(!ws.isClosed)
ws.send("fileUpdate")
})
}
그러면 오류가 해결되지만 원인은 해결되지 않습니다. 귀하의 ob.on('fileUpdate')
이벤트가 발사됩니다 후 소켓이 닫혀 있습니다. WebSocket close 이벤트에서 해당 리스너를 지워야합니다.ws.isWebSocketCloseEvent
import { acceptWebSocket, isWebSocketCloseEvent } from "https://deno.land/[email protected]/ws/mod.ts";
/* ... */
for await (const e of ws) {
if(isWebSocketCloseEvent(e) || e === 'close') {
// clear listeners here
ob.remove("fileUpdate")
// if e === 'close' you may want to close the socket
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다