javascript 如何在 Node 中捕获和处理“WebSocket 已处于 CLOSING 或 CLOSED 状态”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48472977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:03:38  来源:igfitidea点击:

How to catch and deal with "WebSocket is already in CLOSING or CLOSED state" in Node

javascriptnode.jssocketswebsocket

提问by adelriosantiago

I've been searching for a solution to the issue "WebSocket is already in CLOSING or CLOSED state" and found this:

我一直在寻找“WebSocket 已处于 CLOSING 或 CLOSED 状态”问题的解决方案,并发现了这一点:

  1. Meteor WebSocket is already in CLOSING or CLOSED state error
  2. WebSocket is already in CLOSING or CLOSED state.
  1. Meteor WebSocket 已经处于 CLOSING 或 CLOSED 状态错误
  2. WebSocket 已经处于 CLOSING 或 CLOSED 状态。

Answer #1 is for strictly related to Meteor and #2 has no answers... I have a Node server app with a socket:

答案 #1 与 Meteor 严格相关,#2 没有答案......我有一个带套接字的 Node 服务器应用程序:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(socket) {
  socket.on('message', function incoming(data) {
    console.log('Incoming data ', data);
  });
});

And clients connect like this:

客户端连接如下:

const socket = new WebSocket('ws://localhost:3090'); //Create WebSocket connection

//Connection opened
socket.addEventListener('open', function(event) {
  console.log("Connected to server");
});

//Listen to messages
socket.addEventListener('message', function(event) {
  console.log('Message from server ', event);
});

However after a few minutes, clients randomly disconnect and the function

然而几分钟后,客户端随机断开连接,功能

socket.send(JSON.stringify(data));

Will then throw a "WebSocket is already in CLOSING or CLOSED state.".

然后会抛出“WebSocket 已经处于 CLOSING 或 CLOSED 状态。”。

I am looking for a way to detect and deal these disconnections and immediately attempt to connect again.

我正在寻找一种方法来检测和处理这些断开连接并立即尝试再次连接。

What is the most correct and efficient way to do this?

执行此操作的最正确和最有效的方法是什么?

回答by yeya

The easiest way is to check if the socket is open or not before sending.

最简单的方法是在发送之前检查套接字是否打开。

For example - write a simple function:

例如 - 编写一个简单的函数:

function isOpen(ws) { return ws.readyState === ws.OPEN }

Then - before any socket.sendmake sure it is open:

然后 - 在任何之前socket.send确保它是打开的:

if (!isOpen(socket)) return;
socket.send(JSON.stringify(data));

You can also rewrite the sendfunction like this answerbut in my way you can log this situations.

您也可以send这个答案一样重写函数,但按照我的方式,您可以记录这种情况。

And, for your second request

而且,对于您的第二个请求

immediately attempt to connect again

立即尝试再次连接

There is no way you can do it from the server.

您无法从服务器执行此操作。

The client code should monitor the WebSocket state and apply reconnect method based on your needs.

客户端代码应监控 WebSocket 状态并根据您的需要应用重新连接方法。

For example - check thisVueJS library that do it nicely. Look at Enable ws reconnect automaticallysection

例如 - 检查这个做得很好的 VueJS 库。查看启用 ws 自动重新连接部分