websocket-heart-beat-mytest
v1.0.1
Published
```bash npm install websocket-heart-beat-mytest ```
Downloads
1
Maintainers
Readme
安装
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");
};
API
属性
| 属性 | 必填 | 类型 | 默认值 | 描述 | | -------------- | ----- | ------ | ----------- | ------------------------ | | 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);
方法
发送消息
ws.send("Hello World");
断开连接
ws.destroyed();