当服务器宕机时,阻止socket.io在浏览器中抱怨

问题描述 投票:0回答:2

我正在尝试使用socket.io。

场景:

我打开了几个最初连接到服务器的选项卡。我停止服务器是因为我想对代码做一些工作。浏览器开始记录:

polling-xhr.js:206 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NhdlRB- net::ERR_CONNECTION_REFUSED

有什么办法可以抑制这个错误吗?当这种情况发生时,它会在每个浏览器选项卡中使用大量系统资源,特别是当浏览器在该状态下运行很长时间时。

javascript socket.io
2个回答
1
投票

你可以

socket.disconnect()
;出错,否则会尝试重新连接。


0
投票

我重新审视了这个主题,希望它在 2024 年有所改变,尝试作为 Manager 或普通客户端监听 API 中的每个事件,但仍然一无所获,然后我再次遇到这个,叹息:

Please note that error logs such as:

- net::ERR_INTERNET_DISCONNECTED
- net::ERR_CONNECTION_REFUSED
- WebSocket is already in CLOSING or CLOSED state
- Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at xxx. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
- The connection to xxx was interrupted while the page was loading

are not emitted by the Socket.IO library but by the browser itself, and are therefore out of our control.

文档参考:https://socket.io/docs/v3/logging-and-debugging/#error-logs-in-the-browser-console

Github 问题参考:https://github.com/socketio/socket.io-client/issues/1178

#浏览器错误

© www.soinside.com 2019 - 2024. All rights reserved.