拓展 mqtt协议先容
1、安装依赖包
2、项目中引入
- import mqtt from 'mqtt'
复制代码 3、使用
- var client = mqtt.connect('ws//****:8083', { // ws这个为毗连范例,尚有wss 为加密型 connectTimeout: 40000, clientId: '', username: 'user', password: 'password', clean: true }) client.on('connect', (e) => { // console.log("毗连乐成!!!", e) client.subscribe(['subtitle1','subtitle2'], {qos: 0}, (error) => { // qos 为通道 if (!error) { console.log('订阅乐成') } else { console.log('订阅失败') } }) }) // 吸收消息处理惩罚 client.on('message', (topic, res) => { // console.log('收到来自', topic, '的消息', JSON.stringify(res)) const resData = JSON.parse(res.toString()) // 处理惩罚返回来的数据 console.log(resData ) }) // 关闭 // client.end()
复制代码 来源:https://blog.csdn.net/super__code/article/details/111994731
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |