websocket-heart-beat-mytest
1.0.1 • Public • Published
npm install websocket-heart-beat-mytest
import WebsocketHB from " websocket-heart-beat-mytest";
const ws = new WebsocketHB({
url: "ws://xxx",
});
ws.onopen = () => {
console.log("connect success");
};
ws.onmessage = (e) => {
console.log(`onmessage: ${e.data}`);
};
ws.onerror = () => {
console.log("connect onerror");
};
ws.onclose = () => {
console.log("connect onclose");
};
属性 |
必填 |
类型 |
默认值 |
描述 |
url |
true |
string |
none |
websocket 服务端接口地址 |
pingTimeout |
false |
number |
8000 |
心跳包发送间隔 |
reconnectLimit |
false |
number |
15 |
重连尝试次数 |
pingMsg |
false |
string |
"heartbeat" |
心跳包消息 |
const opts = {
url: "ws://xxx",
pingTimeout: 8000, // 发送心跳包间隔,默认 8000 毫秒
reconnectLimit: 10, // 最大重连次数
pingMsg: "heartbeat", // 心跳包的消息内容
};
const ws = new WebsocketHB(opts);
Package Sidebar
Install
npm i websocket-heart-beat-mytest
Weekly Downloads