ws-heartbeat-ts
v0.6.0
Published
简单封装WebSocket的心跳检测和自动连接
Downloads
14
Maintainers
Readme
ws-heartbeat-ts
According to websocket-heartbeat-js changed TS version.
Why write TS version
- Want to try, pack the TS file, and automatically generate
.d.ts
😀 - Typescript can use
singleton
mode to avoid the situation that the client creates two identical WS links( version >= 0.5.x)
How to use
- Use the
getInstance
method to get the instance, not thenew
keyword. - Because this can ensure that for the same URL address, when it is not the initial call, it will get the existing instance, instead of creating a WS connection of the same URL.
/** ts */
import WsHeartBeat, { FWsData } from 'ws-heartbeat-ts';
const wsInstance: WsHeartBeat = WsHeartBeat.getInstance(opt);
wsInstance.onmessage = (data: FWsData) => {
console.log(data, 'this is ws data');
};
Option
| Attribute | required | type | default | description | | ------ | ------ | ------ | ------ | ------ | | url | true | string | none | websocket service address | | pingTimeout | false | number | 5000 | A heartbeat is sent every 5 seconds. If any backend message is received, the timer will reset | | pongTimeout | false | number | 10000 | After the Ping message is sent, the connection will be disconnected without receiving the backend message within 10 seconds | | reconnectTimeout | false | number | 2000 | The interval of reconnection | | pingMsg | false | string | "{}" | Ping message value | | repeatLimit | false | number | 5 | The trial times of reconnection | | dataType | false | json/byte | 'json' | How to process the returned data | | byteFormat | false| utf-8/iso-8859-2/koi8/cp1261/gbk/etc | 'utf-8'| If datatype is byte, which encoding is used for parsing |
Appointment
- If
repeatLimit
default isnull
, considering that the new connection will knock down the old connection, if it is not handled, there will be two clients pushing each other and infinitely reconnecting. The back end is required to send the logged in message. - dataType
interface FWsData { code: string; // 识别码 data: unknown; // 应该用泛型 type?: string; // 在code码相同的情况下的备用选项 message?: string; // 需要提示的其他信息 }
Difference
defaultValue
pingTimeout
:15000
=>5000
pingMsg
:heartbeat
=>{}
params —— Not yet
- Add parameters
boolean
. It may be used in the future
- Add parameters
window.setTimeout —— Not yet
Considering that
window.settimeout
andwindow.setinterval
will have problems when the browser is minimized, we are ready to switch to worker-timersinterface FWsData,class WsData
Front and back end data transfer specification - JSON typesendData()
Add a method to processJSON
.sendData
can instantiate objects in a fixed format and parse them in the way ofWsData
. At the same time, the original message event object is reserved as the second return valueReserved
byte
transmission mode exceptJSON
format (it may be used if the back end isgolong
)others Please see the specific code
Remark
Please refer to websocket-heartbeat-js for other detailed documents
Change declaration file
Just run npm run build
in the root of the project.
After packaging, the related declaration file will be generated automatically. .d.ts