moscamqtt在azure上的经纪人



我正在学习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();
});

提前感谢,

我遇到了同样的问题。

您必须使用Websockets上的MQTT,服务器地址为:

var client  = mqtt.connect('ws://<app-id>.azurewebsites.net');

您需要在azure->Web应用程序->设置->应用程序设置->Web套接字中激活Web套接字。

我希望这能有所帮助。

这里是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();
});

我希望这能有所帮助。

我认为这听起来像是当你忘记取消订阅然后重新订阅某个主题时发生的问题。

(例如)当客户端和服务器正在运行,然后关闭服务器(导致与客户端断开连接),然后重新启动服务器,从而导致客户端重新连接(和订阅)时,可能会发生这种情况。与此相比:

  1. 服务器启动->客户端连接->客户端订阅
  2. 服务器关闭,客户端不取消订阅
  3. 服务器启动->客户端连接->客户端订阅(第二次)
  4. 这将导致两次/三次/。。。消息

这是再现你问题的原因吗?

相关内容

  • 没有找到相关文章

最新更新