Mr.*_* Jo 6 javascript node.js
我目前正在开发 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);
});
Run Code Online (Sandbox Code Playgroud)
我认为一个主要问题是没有 ping 方法:
client.on('open') -> client.onopen available in JavaScript
client.on('close') -> client.onclose available in JavaScript
client.on('ping') -> How? Just how?
Run Code Online (Sandbox Code Playgroud)
小智 4
没有 Javascript API 可以发送 ping 帧或接收 pong 帧。您的浏览器要么支持,要么不支持。也没有 API 可以启用、配置或检测浏览器是否支持以及正在使用 ping/pong 框架。
| 归档时间: |
|
| 查看次数: |
14937 次 |
| 最近记录: |