【发布时间】:2016-07-12 15:28:15
【问题描述】:
我正在学习 MQTT,并希望将开源 mosca 代理部署到一个运行 mosca 的天蓝色 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();
});
提前致谢,
【问题讨论】: