# yskj-mqtt

> 元素科技mqtt层封装

Latest version **1.1.4** (published 2021-11-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install yskj-mqtt
pnpm add yskj-mqtt
yarn add yskj-mqtt
bun add yskj-mqtt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2021-11-12 |
| First published | 2021-04-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | liuqing |
| Maintainers | liuqing67 |
| Keywords | mqtt |

## Links

- npm: https://www.npmjs.com/package/yskj-mqtt
- npm.io page: https://npm.io/package/yskj-mqtt

## Dependencies (1)

- [mqtt](https://npm.io/package/mqtt.md) ^4.2.6

## Recent versions

- 1.1.4 (latest) — 2021-11-12
- 1.1.3 — 2021-11-11
- 1.1.2 — 2021-04-23
- 1.1.1 — 2021-04-23
- 1.1.0 — 2021-04-22
- 1.0.9 — 2021-04-21
- 1.0.8 — 2021-04-21
- 1.0.7 — 2021-04-19
- 1.0.6 — 2021-04-19
- 1.0.5 — 2021-04-15
- 1.0.4 — 2021-04-15
- 1.0.3 — 2021-04-15
- 1.0.2 — 2021-04-15
- 1.0.1 — 2021-04-15
- 1.0.0 — 2021-04-15

## README

#### 最新版本 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的唯一用户值，<font color='red'> `* V1.0.9取消` </font>|
| connectTimeout | 否 | Number | 4000 | 设置连接超时时间 |
| token | 是 | String | '' | 用户登录mqtt令牌 |
| clean | 否 | Boolean | true | 是否在重连恢复时订阅 |
| url | 是 | String | '' | 连接mqtt地址 |
| userName | 否 | String | 'web_user' | 用户名 |
| theme | 否 | Array | [obj] | 订阅的主题，默认订阅'ys/web'主题。格式为json数组，配置项见下文theme配置，<font color='red'> `* V1.0.9下为必传` </font> |


#### theme配置（支持扩展自定义值，会在连接成功回调返回）
| 配置项|是否必传|类型|默认值|说明|
|-|:-:|:-:|:-:|:-:|
| type | 是 | String | '' | 主题类型，最后不要以'/'结尾，切记！！！ |
| ... | 否 | String | '' | 扩展的自定义的值 |

#### 回调函数
所有的回调函数是在on中接收

| 函数名|说明|
|-|:-|
| connect | 成功连接mqtt时返回，返回参数为当前订阅的主题类型 |
| message | mqtt成功下发数据时返回，返回的为json对象 |
| reconnecting | 正在重新连接时返回，返回参数为：第几次重新连接 |
| error | mqtt错误时返回 |

###### 事件
eg: this.myMqtt.closeMqtt()

| 事件名|事件说明|
|-|:-|
| closeMqtt[callback] | 断开mqtt |
| reconnect | 手动重连mqtt |

---
_Source: https://npm.io/package/yskj-mqtt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
