我目前正在开发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?
答案 0 :(得分:1)
我认为您在客户端上寻找的是onmessage
:
client.onmessage = function (event) {
console.log(event.data);
}
从服务器发送的所有消息都可以通过这种方式收听。参见https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
答案 1 :(得分:0)
没有Javascript API发送ping帧或接收pong帧。您的浏览器是否支持此功能。也没有用于启用,配置或检测浏览器是否支持并正在使用ping / pong框架的API。
答案 2 :(得分:0)
遗憾但确实如此,在 ping frame 的情况下,API 不支持之前的答案中提到的它。
最流行的解决方法是监听关闭事件并尝试使用间隔重新连接到服务器。
This tutorial 易于理解并包含大多数以 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();
};