我正在学习 MQTT,并希望将开源 mosca 代理部署到运行 mosca 的 azure Web 应用程序,无需数据库(不需要任何涉及持久性的 QoS)。
我使用了 http://thejackalofjavascript.com/getting-started-mqtt/ 中的代码这是一个很棒的本地部署教程(见下文)
var mosca = require('mosca')
var settings = {
port: 1883
};
//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running')
}
// fired when a client is connected
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published : ', packet.payload);
});
// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
console.log('subscribed : ', topic);
});
// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
console.log('unsubscribed : ', topic);
});
// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
console.log('clientDisconnecting : ', client.id);
});
// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
console.log('clientDisconnected : ', client.id);
});
我可以在 Azure 网站上运行此代码,但不知道如何在使用 MQTT 的客户端中设置此代理的地址和端口 - 请参阅下文
var mqtt = require('mqtt')
client = mqtt.connect([{port:1883, host:'???'}]); //what do you use here as the port and server address here instead of localhost and 1883? I tried using the URL for the web app in azure but it does not work and i do not get any error messages.
client.on('connect', function () {
console.log('client connected');
client.subscribe('presence');
client.publish('presence', 'Hello mqtt');
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
client.end();
});
提前致谢,
最佳答案
我也遇到了同样的问题。
您必须使用MQTT over Websockets , 服务器地址为:
var client = mqtt.connect('ws://<app-id>.azurewebsites.net');
并且您需要在 azure -> Web 应用 -> 设置 -> 应用程序设置 -> Web 套接字中激活 Websockets。
我希望这能有所帮助。
关于azure - Azure 上的 Mosca Mqtt 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210370/