55 lines
875 B
Markdown
55 lines
875 B
Markdown
# celery-mqtt
|
||
|
||
> 第一次发布插件,摸索学习中
|
||
|
||
- 进入项目根目录下安装mqtt,执行命令
|
||
-
|
||
```sh
|
||
npm install mqtt --save
|
||
```
|
||
|
||
- 在页面引入mqtt
|
||
```javascript
|
||
import mqtt from '@/uni_modules/celery-mqtt/js_sdk/index.js';
|
||
```
|
||
|
||
- 连接订阅
|
||
```javascript
|
||
mqtt.connect('127.0.0.1', 8083,
|
||
// 连接选项
|
||
{
|
||
// 用户名
|
||
username: 'admin',
|
||
// 密码
|
||
password: '1234.abcd',
|
||
// 自动重连间隔,设置为0表示禁用重连
|
||
reconnectPeriod: 0
|
||
},
|
||
// 订阅主题,字符串或者数组均支持
|
||
'Communication',
|
||
// 订阅选项
|
||
{
|
||
qos: 0
|
||
},
|
||
// 订阅消息回调
|
||
(topic, message) => {
|
||
console.log('received:', topic, message);
|
||
});
|
||
```
|
||
|
||
- 发布消息
|
||
```javascript
|
||
mqtt.publish(
|
||
// 发布主题,字符串
|
||
'Command',
|
||
// 发布选项
|
||
{
|
||
qos: 0
|
||
},
|
||
// 消息
|
||
'test',
|
||
// 发布回调
|
||
(topic) => {
|
||
console.log('published:', topic);
|
||
});
|
||
``` |