azure - Azure 上的 Mosca Mqtt 代理

标签 azure mqtt broker

我正在学习 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/

相关文章:

azure - TransactionScope 和 Azure 表存储

python - paho mqtt 中的 on_message 是否在新线程中运行?

android - Android 上的 MQTT Eclipse Paho 客户端,断开连接挂起并且永远不会完成

java - Interactive Brokers Java API 获得所有职位?

azure - 机器人框架 : httpStatusCode": 504 getting Failed to send activity: bot timed out

Azure 数据工厂复制事件无法将字符串(从 csv)映射到 Azure SQL 表接收器 uniqueidentifier 字段

azure - azure资源管理器模板的资源部分(资源数组)中的 "apiVersion"属性有什么用?

javascript - 连接 Html 表单中的用户输入并为其分配 ID,以便在 JavaScript 中提交

xml - 将 activemq 代理配置拆分为多个文件

java - 当 ActiveMQ 启动时警告为 java.io.EOFException