【问题标题】:mosca mqtt broker on azure天蓝色上的 mosca mqtt 经纪人
【发布时间】: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();
});

提前致谢,

【问题讨论】:

    标签: azure mqtt broker


    【解决方案1】:

    我遇到了同样的问题。

    你必须使用MQTT over Websockets, 服务器地址是:

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

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

    我希望这会有所帮助。

    【讨论】:

    • 拉赫达尔,谢谢。这有很大帮助。由于您的洞察力,我现在可以正常工作。我必须按照步骤通过调用您建议的链接中所示的 Server#attachHttpServer 方法以及使用 var client = mqtt.connect('ws:/ /.azurewebsites.net');建议你让它工作!再次感谢
    • @bulho 你能分享你的解决方案吗?我做不出来
    【解决方案2】:

    这是 Azure 上的代理(已激活 websockets 的网络应用)

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

    我希望这会有所帮助。

    【讨论】:

    • 我还需要做什么吗?我做不出来
    【解决方案3】:

    我认为这听起来像是您忘记取消订阅然后重新订阅主题时出现的问题。

    这可能发生(例如)当您让客户端和服务器运行,然后关闭服务器(导致与客户端断开连接)然后重新启动服务器,这导致客户端重新连接(和订阅)。比较一下:

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

    这是重现您的问题的原因吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多