我正在学习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();
});提前谢谢你,
发布于 2016-04-12 03:34:13
我也有同样的问题。
您必须使用MQTT over Websockets,服务器地址为:
var client = mqtt.connect('ws://<app-id>.azurewebsites.net');您还需要在azure -> Web应用程序->设置->应用程序设置-> Websockets中激活Websockets。
我希望这能有所帮助。
发布于 2016-04-24 02:08:25
这是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();
});我希望这能帮到你。
发布于 2016-11-25 23:09:28
我认为这听起来像是当你忘记取消订阅然后重新订阅一个主题时发生的问题。
例如,当您运行客户端和服务器,然后关闭服务器(这会导致断开与客户端的连接),然后重新启动服务器,从而导致客户端重新连接(和订阅)时,可能会发生这种情况。与此进行比较:
这就是重现你问题的原因吗?
https://stackoverflow.com/questions/36210370
复制相似问题