【发布时间】:2020-10-05 05:05:28
【问题描述】:
我正在学习 MQTT,并将开源 mosca 代理部署到一个天蓝色的 Web 应用程序。问题是代理在 2 台之间工作,但是当我尝试将我的 arduino nano 连接到代理时,它总是失败。这是我的服务器代码:
//Mosca
const mosca = require('mosca')
const settings = {
http: {
// port for websockets, MQTT is running in default port 1883
port: 8000,
bundle: true,
static: './public'
}
}
// start mosca
const moscaServer = new mosca.Server(settings)
moscaServer.on('ready', setup)
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running in port 1883!')
console.log('Using port 8000 for MQTT over Web-Sockets!')
}
// fired when a client is connected
moscaServer.on('clientConnected', function(client) {
console.log('client connected', client.id)
})
// fired when a message is received
moscaServer.on('published', function(packet, client) {
//if (packet.topic == '/example') {
console.log(packet.payload.toString('utf-8'))
//}
})
// fired when a client subscribes to a topic
moscaServer.on('subscribed', function(topic, client) {
console.log('subscribed : ', topic)
})
// fired when a client unsubscribes to a topic
moscaServer.on('unsubscribed', function(topic, client) {
console.log('unsubscribed : ', topic)
})
// fired when a client is disconnecting
moscaServer.on('clientDisconnecting', function(client) {
console.log('clientDisconnecting : ', client.id)
})
// fired when a client is disconnected
moscaServer.on('clientDisconnected', function(client) {
console.log('clientDisconnected : ', client.id)
})
【问题讨论】:
-
鉴于您说这可以在 PC 上运行,为什么您认为 mosca 代码有问题,而不是 Arduino 中运行的代码?
-
是的,hardilib 说的。哪个代码有错误?发布该代码!而且,告诉我们“总是失败”的真正含义是什么?什么错误?有错误代码吗?怎么会失败?!?
标签: arduino mqtt azure-web-app-service broker mosca