Vue中使用mqtt

官方文档

使用

let mqtt = require('mqtt');
let client = {};
//开启mqtt链接
client = mqtt.connect('ws://192.168.0.0/mqtt', {
    port: 8083,
    clientId: 'xxx',
    username: "xxx",
    password: "xxx",
    clean: false,
});
//订阅后端给你发的字段 在on里面接收
client.on('connect', function () {
    client.subscribe('订阅的字段', function (err) {
        if (!err) {
            //DOSOMETHINGS
        }
    })
});

//接收消息
client.on('message', (topic, message) => {
    console.log('in message');
    if (topic === '订阅的字段') {
    //DOSOMETHING
    }
})
//连接断开
client.end()

你可能感兴趣的:(vue.js,前端)