1.0.6 • Published 5 years ago

websockethb v1.0.6

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

websockethb

Author QQ Email

食用方式

安装

npm install websockethb

引入与使用

import WebsocketHB from 'websockethb'

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心跳包发送间隔
pongTimeoutfalsenumber1500015 秒内没收到后端消息便会认为连接断开
reconnectTimeoutfalsenumber4000尝试重连的间隔时间
reconnectLimitfalsenumber15重连尝试次数
pingMsgfalsestring"heartbeat"心跳包消息
const opts = {
  url: 'ws://xxx',
  pingTimeout: 8000, // 发送心跳包间隔,默认 8000 毫秒
  pongTimeout: 15000, // 最长未接收消息的间隔,默认 15000 毫秒
  reconnectTimeout: 4000, // 每次重连间隔
  reconnectLimit: 15, // 最大重连次数
  pingMsg: 'heartbeat' // 心跳包的消息内容
}
const ws = new WebsocketHB(opts)

方法

发送消息

ws.send('Hello World')

断开连接

ws.destroyed()

获取实例

获取创建的 websocket 实例,一般情况下不推荐使用该方法。

ws.getWSInstance()
1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago