A client-websocket written in TypeScript for browser-applications. Focus is on simplicity, reliability and extensibility. It provides convenient features to automatically reconnect, Support multiple connections at the same time and Manual destruction.
npm install websocket-easy-js --save
or
yarn add websocket-easy-js
import { Socket } from 'websocket-easy-js'
// 参数配置,见下面参数属性
let config = {
....
}
// 创建对象
this.wbSocket = new Socket(config)
// 连接
this.wbSocket.connection()
// 发送信息
this.wbSocket.sendMessage(message)
//关闭(销毁)
this.wbSocket.close()
...
config对象中有以下属性
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
url | string | null | 必填,链接的通道的地址 |
heartTime | number | 5000 | 心跳时间间隔 |
heartMsg | string | 'ping' | 心跳信息 |
isReconnect | boolean | true | 是否自动重连 |
reconnectTime | number | 2000 | 重连时间间隔 |
reconnectCount | number | -1 | 重连次数 -1 则不限制 |
openCb | function | - | 连接成功的回调 |
closeCb | function | - | 关闭的回调 |
messageCb | function | - | 消息的回调 |
errorCb | function | - | 错误的回调 |