如何在JavaScript和NodeJS WebSocket之间进行Ping/Pong?

6

我正在开发一个NodeJS WebSocket服务器。为了检测断开的连接,我按照这里的指南进行操作:

https://github.com/websockets/ws#how-to-detect-and-close-broken-connections

服务器端运行非常好,但是客户端存在问题,因为我找不到ping功能。

有没有人有办法在不使用库的情况下完成客户端部分?

const WebSocket = require('ws');

function heartbeat() {
  clearTimeout(this.pingTimeout);

  // Use `WebSocket#terminate()`, which immediately destroys the connection,
  // instead of `WebSocket#close()`, which waits for the close timer.
  // Delay should be equal to the interval at which your server
  // sends out pings plus a conservative assumption of the latency.
  this.pingTimeout = setTimeout(() => {
    this.terminate();
  }, 30000 + 1000);
}

const client = new WebSocket('wss://echo.websocket.org/');

client.on('open', heartbeat);
client.on('ping', heartbeat);
client.on('close', function clear() {
  clearTimeout(this.pingTimeout);
});

一个主要问题是我认为没有ping方法:

client.on('open') -> client.onopen available in JavaScript
client.on('close') -> client.onclose available in JavaScript
client.on('ping') -> How? Just how?

你可以直接使用全局的 WebSocket - VLAZ
这就是我正在做的事,但我没有找到ping方法。请完整地阅读我的问题。 - Mr. Jo
这个回答解决了你的问题吗?从浏览器发送WebSocket ping/pong帧 - TheGr8_Nik
3个回答

4
没有 JavaScript API 可以发送 ping 帧或接收 pong 帧。这要么得到浏览器支持,要么不行。也没有 API 可以启用、配置或检测浏览器是否支持并使用 ping/pong 帧。

https://dev59.com/rmkv5IYBdhLWcg3wdQhT#10586583


1

有点遗憾,对于ping frame,API并不支持它,正如之前的答案所提到的。

最流行的解决方法是监听关闭事件,并使用间隔尝试重新连接到服务器。

这个教程易于理解,包含了大多数使用WS的情况:

var ws = new WebSocket("ws://localhost:3000/ws");
let that = this; // cache the this
var connectInterval;
var check = () => {
    const { ws } = this.state;
    if (!ws || ws.readyState == WebSocket.CLOSED) this.connect(); //check if websocket instance is closed, if so call `connect` function.
};

// websocket onopen event listener
ws.onopen = () => {
    console.log("connected websocket main component");

    this.setState({ ws: ws });
    that.timeout = 250; // reset timer to 250 on open of websocket connection 
    clearTimeout(connectInterval); // clear Interval on on open of websocket connection
};

// websocket onclose event listener
ws.onclose = e => {
    console.log(
        `Socket is closed. Reconnect will be attempted in ${Math.min(
            10000 / 1000,
            (that.timeout + that.timeout) / 1000
        )} second.`,
        e.reason
    );

    that.timeout = that.timeout + that.timeout; //increment retry interval
    connectInterval = setTimeout(this.check, Math.min(10000, that.timeout)); //call check function after timeout
};

// websocket onerror event listener
ws.onerror = err => {
    console.error(
        "Socket encountered error: ",
        err.message,
        "Closing socket"
    );
    ws.close();
};

-3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接