1. 安装
npm install paho-mqtt -s
2. 初始化
const PahoMQTT = require('paho-mqtt')
const name = new Date().getTime() + 'client'
const client = new PahoMQTT.Client('www.100link.net', Number(61615), name)
client.connect({
invocationContext: {
host: 'www.100link.net ',
port: '61615',
path: client.path,
clientId: 'clientId'
// eslint-disable-next-line no-trailing-spaces
},
useSSL: true,
onSuccess: function(res) {
client.subscribe('offlineTopic') // 订阅频道
client.subscribe('realTimeCoordinateTopic')
}
})
3. 接收消息
client.onMessageArrived = onMessageArrived // 接收消息
function onMessageArrived(message) {
if (message.destinationName === 'offlineTopic') {
commit('SAT_WARNING', message.payloadString)
}
if (message.destinationName === 'realTimeCoordinateTopic') {
commit('SAT_POINT', message.payloadString)
}
}
4. 发送消息
client.publish('toAppTopic', info)
5. 停止监听
client.unsubscribe('realTimeCoordinateTopic')
client.unsubscribe('offlineTopic')
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/230911.html原文链接:https://javaforall.cn