npm.io
1.1.4 • Published 4 years ago

yskj-mqtt

Licence
ISC
Version
1.1.4
Deps
1
Size
7 kB
Vulns
0
Weekly
0

最新版本 V1.1.4

更新说明(V1.1.4)

1. 添加mqtt心跳
更新说明(V1.1.3)
1. 删除重连次数大于10关闭mqtt的代码
更新说明(V1.1.2)
1. 新增接收消息回调topic参数
2. 主题去重默认主题
3. 整理多余的代码
更新说明(V1.1.1)
1. 修改多个订阅主题时,自定义的主题为用户方传入,不手动拼接clintId
更新说明(V1.1.0)
1. 修改接收参数回调少写了一个this
更新说明(V1.0.9)
1. 取消clintId参数,由底层维护此字段
2. 调整theme参数集合为非必传,默认订阅'ys/web'主题
更新说明(V1.0.8)
1. 修复关闭mqtt时不传递回调函数报错问题
更新说明(V1.0.7)
1. 新增closeMqtt回调函数
2. 修复连接成功时返回的主题类型为undefined
更新说明(V1.0.6)
1. 新增userName字段 
2. 解决已连状态下重连bug
基本用法
    options = {
        connectTimeout: 4000,
        clientId: 'wb_' + 111111,
        token: 'xxx',
        clean: true,
        url: 'ws://192.168.38.218:28083/mqtt',
        theme: [{
            type: 'ys/web'
        }]
    }
    this.myMqtt = new mqtt(options)
    this.myMqtt.on('connect', (data) => {
        console.log('订阅成功', data)
    })
    this.myMqtt.on('message', (data) => {
        console.log('成功接收消息', data)
    })
options说明
配置项 是否必传 类型 默认值 说明
clientId 否 String '' 订阅mqtt的唯一用户值, * V1.0.9取消
connectTimeout 否 Number 4000 设置连接超时时间
token 是 String '' 用户登录mqtt令牌
clean 否 Boolean true 是否在重连恢复时订阅
url 是 String '' 连接mqtt地址
userName 否 String 'web_user' 用户名
theme 否 Array [obj] 订阅的主题,默认订阅'ys/web'主题。格式为json数组,配置项见下文theme配置, * V1.0.9下为必传
theme配置(支持扩展自定义值,会在连接成功回调返回)
配置项 是否必传 类型 默认值 说明
type 是 String '' 主题类型,最后不要以'/'结尾,切记!!!
... 否 String '' 扩展的自定义的值
回调函数

所有的回调函数是在on中接收

函数名 说明
connect 成功连接mqtt时返回,返回参数为当前订阅的主题类型
message mqtt成功下发数据时返回,返回的为json对象
reconnecting 正在重新连接时返回,返回参数为:第几次重新连接
error mqtt错误时返回
事件

eg: this.myMqtt.closeMqtt()

事件名 事件说明
closeMqtt[callback] 断开mqtt
reconnect 手动重连mqtt

Keywords