# very-mqtt-sdk

> ## 示例用法

Latest version **0.0.8** (published 2019-07-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install very-mqtt-sdk
pnpm add very-mqtt-sdk
yarn add very-mqtt-sdk
bun add very-mqtt-sdk
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2019-07-16 |
| First published | 2019-07-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 291.6 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | 高峰 |
| Maintainers | verymuch |

## Links

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

## Dependencies (2)

- [mqtt](https://npm.io/package/mqtt.md) ^3.0.0
- [axios](https://npm.io/package/axios.md) ^0.19.0

## Recent versions

- 0.0.8 (latest) — 2019-07-16
- 0.0.7 — 2019-07-16
- 0.0.6 — 2019-07-16
- 0.0.5 — 2019-07-09
- 0.0.4 — 2019-07-09
- 0.0.3 — 2019-07-09
- 0.0.2 — 2019-07-09
- 0.0.1 — 2019-07-09

## README

# mqtt-sdk使用文档

## 示例用法

```
// 创建新实例
const psconn = new PSConn()
// 建立连接
psconn.connect(
  'mqttbroker', 
  'gf', 
  'C15D9FA90F28534643F863A3D446E0C3', 
  '1561517671717',
)

// 监听连接成功实践，在其内订阅和发布消息
psconn.on('connect', (e) => {
  if(e) {
    console.error(e)
    return
  }

  psconn.sub(
    'mqttbroker/test-topic1',
    'B75FCB6455669502DB60243459AB93B3',
    '1561518748069'
  )

  setTimeout(() => {
    psconn.pub(
      'mqttbroker/test-topic1',
      'test-topic1：message1',
      'B75FCB6455669502DB60243459AB93B3',
      '1561518748069'
    )
    psconn.unsub('mqttbroker/test-topic1')
  }, 2000)

  setTimeout(() => {
    psconn.pub('mqttbroker/test-topic1', 'test-topic1：message2', 'B75FCB6455669502DB60243459AB93B3', '1561518748069')
  }, 4000)
})

// 监听消息，回调函数中包括topic和message参数
psconn.on('message', (topic, message) => {
  console.log(topic, message.toString())
})

// 监听连接关闭事件，手动触发disconnect后会触发
psconn.on('close', () => {
  console.log('连接已关闭！')
})

// 监听错误
psconn.on('error', (e) => {
  console.error(e)
})
```

## 术语说明

* appkey：业务线唯一标识
* deviceName：产品唯一标识，业务线下唯一
* userSign：用户签名，其中签名规则参照[签名规则](http://showdoc.lg.opsdev.qihoo.net:8360/web/#/11?page_id=231)
* random：签名随机字符串，不超过20byte


## 相关接口说明

### 1. connect 建立连接

`connect(appkey, deviceName, userSign, random)`

该方法用于建立连接，共包括四个参数，分别为appkey, deviceName, userSign, random。

### 2. disconnect 断开连接

`disconnect()`

### 3. sub 订阅消息

`sub(topic, userSign, random)`

该方法用于发布消息，其中topic为消息主题。

### 4. pub 发布消息

`pub(topic, payload, userSign, random)`

该方法用于发布消息，其中topic为消息主题，payload为消息内容。

### 5. unsub 订阅消息

`unsub(topic)`

该方法用于取消发布消息，其中topic为消息主题。

### 6. on 事件监听

`on(type, handler)`

该方法用于监听连接过程中的事件，第一个参数是事件类型type(支持connect/message/close/error)，第二个参数为回调函数。具体使用方法见上面的示例代码。

同一类型的事件可以多次监听。

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