1.0.1 • Published 2 years ago

websocket-heart-beat-mytest v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

安装

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

属性

属性必填类型默认值描述
urltruestringnonewebsocket 服务端接口地址
pingTimeoutfalsenumber8000心跳包发送间隔
reconnectLimitfalsenumber15重连尝试次数
pingMsgfalsestring"heartbeat"心跳包消息
const opts = {
  url: "ws://xxx",
  pingTimeout: 8000, // 发送心跳包间隔,默认 8000 毫秒
  reconnectLimit: 10, // 最大重连次数
  pingMsg: "heartbeat", // 心跳包的消息内容
};
const ws = new WebsocketHB(opts);

方法

发送消息

ws.send("Hello World");

断开连接

ws.destroyed();
1.0.1

2 years ago

1.0.0

2 years ago