我正在学习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();
});
提前致谢,
答案 0 :(得分:0)
我遇到了同样的问题。
你必须使用MQTT over Websockets, 服务器地址为:
<Children ref="children" />
你需要在azure中激活Websockets - &gt;网络应用程序 - &gt;设置 - &gt;应用程序设置 - &gt;网络套接字。
我希望这可以提供帮助。
答案 1 :(得分:0)
这是Azure上的代理(已激活websockets的Web应用程序)
var mosca = require('mosca')
var http = require('http')
, httpServ = http.createServer();
var server = new mosca.Server();
server.on('ready', setup);
server.attachHttpServer(httpServ);
var port = process.env.PORT || 3000;
httpServ.listen(port);
// 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.toString());
console.log('timestamp: ',new Date().getMilliseconds());
});
// 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);
});
以下是与上述代理交互的示例设备。为简单起见,我们有设备发布和订阅主题:
var mqtt = require('mqtt');
var client = mqtt.connect('ws://your_web_app_address_here.azurewebsites.net');
client.on('connect', function () {
client.subscribe('someTopic');
client.publish('someTopic', 'hello from my device!);
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
client.end();
});
我希望这会有所帮助。
答案 2 :(得分:0)
我认为这听起来像是在您忘记取消订阅然后重新订阅主题时发生的问题。
当您运行客户端和服务器,然后关闭服务器(导致与客户端断开连接)然后重新启动服务器(这会导致客户端重新连接(和订阅))时,可能会发生这种情况(例如)。与此相比:
那是什么再现了你的问题?