我有这个演示代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body onload="init()">
<script type="text/javascript">
function init() {
this.ws = new WebSocket("ws://localhost:1234");
console.log("opening");
// on close, display message and try to reopen
this.ws.onclose = function (evt) {
console.log("closed");
}
}
</script>
</body>
</html>
正在连接到不存在的 Websocket 端点。我预计这基本上会立即出错,但在 Firefox 中,打印“打开”和打印“关闭”之间的时间超过一分钟:
这是预期的行为,还是可以将其配置为更快地超时?在 Chrome 中,连接立即出错:
最佳答案
找到了。这是intentional行为符合 RFC 6455。根据 this补丁中,它使用最多 60 秒的指数退避。不幸的是,我认为这意味着 Firefox 中自动重新连接到 websocket 端点的功能被破坏了。
关于javascript - Firefox 在连接错误时不会立即关闭 WebSocket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59548618/