首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >azure上的mosca mqtt代理

azure上的mosca mqtt代理
EN

Stack Overflow用户
提问于 2016-03-25 05:33:36
回答 3查看 624关注 0票数 2

我正在学习MQTT,并希望将开源mosca代理部署到一个运行mosca而不需要数据库的azure web应用程序(不需要任何涉及持久性的QoS )。

我使用了来自http://thejackalofjavascript.com/getting-started-mqtt/的代码,这是一个很好的本地部署教程(见下文)。

代码语言:javascript
运行
复制
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的客户端中设置此代理的地址和端口-请参见下面的内容

代码语言:javascript
运行
复制
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();
});

提前谢谢你,

EN

回答 3

Stack Overflow用户

发布于 2016-04-12 03:34:13

我也有同样的问题。

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

代码语言:javascript
运行
复制
var client  = mqtt.connect('ws://<app-id>.azurewebsites.net');

您还需要在azure -> Web应用程序->设置->应用程序设置-> Websockets中激活Websockets。

我希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2016-04-24 02:08:25

这是Azure上的代理(激活了websockets的web应用程序)

代码语言:javascript
运行
复制
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);
});

这是一个与上面的代理交互的示例设备。为了简单起见,我们让设备发布和订阅主题:

代码语言:javascript
运行
复制
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();
});

我希望这能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2016-11-25 23:09:28

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

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

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

这就是重现你问题的原因吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36210370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档