我正在尝试将Mosca MQTT服务器添加到我现有的Express应用程序中。
我只想向刚连接的客户端发送一条欢迎消息,但它不起作用。
const mqttServ = new mosca.Server({})
mqttServ.attachHttpServer(server)
var message = {
topic: 'helloworld',
payload: 'abcde', // or a Buffer
qos: 0, // 0, 1, or 2
retain: false // or true
}
mqttServ.on('clientConnected', (client) => {
mqttServ.publish(message, client, () => {
console.log('message sent')
})
})在网页上,我把客户端的主题设为'/hello/world‘,但是我什么也没得到。
var client = mqtt.connect()
client.subscribe('helloworld')
client.on('message', (topic, payload) => {
console.log([topic, payload].join(": "))
client.end()
})-更新--
如果我在发布调用中删除客户端,消息将到达网页,但这不是我要尝试做的事情。
发布于 2016-05-22 03:46:20
在subscribe事件之后发布消息起到了作用
mqttServ.on('subscribed', (topic, client) => {
mqttServ.publish(message, client)
})https://stackoverflow.com/questions/37365053
复制相似问题