2016-03-24 16 views
1

私はMQTTを勉強しており、オープンソースのmoscaブローカーをデータベースなしでmoscaを実行しているazure Webアプリケーションにデプロイしたいとします(永続性を含むQoSは必要ありません)。mosca mqtt broker on azure

は、私はAzureのWebサイト上で実行されているこのコードを取得することができ

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

(下記参照)施設の配備にするために偉大なチュートリアルがあるhttp://thejackalofjavascript.com/getting-started-mqtt/からのコードを使用しているが、ようセットアップに何を知りません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(); 
}); 

下記のおかげを参照してください。

答えて

0

は、私は同じ問題がありました。

あなたは 、MQTT over Websocketsを使用する必要があり、サーバーのアドレスは次のようになります。

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

、あなたは紺碧にWebSocketを有効にする必要があり - > [Webアプリケーション] - > [設定] - > [アプリケーション設定] - > [ウェブソケット。

私はこれが役立つことを望みます。ここで

+0

Lakhdar、ありがとう。これは多くの助けとなりました。私はあなたの洞察力のおかげで今働いています。私は、あなたが提案したリンクに示されているようにServer#attachHttpServerメソッドを呼び出すことによって、また、var client = mqtt.connect( 'ws:/')でそれを行うときにMQTT-over-websocket capabilitieを使用してノードHTTPサーバーを補強する手順に従わなければなりませんでした。/ .azurewebsites.net ');あなたはそれが働いたことを示唆しています!もう一度ありがとう – bulho

+0

あなたは大歓迎です:) –

+0

@bulhoあなたはあなたのソリューションを共有してくださいできますか?私はその仕事をすることができません –

0

ここアズール(活性化WebSocketを使用した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(); 
}); 

私はこれが役立つことを望みます。

+0

私は他の何かをしなければなりませんか?私はその仕事をすることができません –

0

これは、退会を忘れてからトピックに再登録するのを忘れたときに発生する問題のように思えます。

これは、クライアントとサーバーを実行してからサーバーをシャットダウンすると(クライアントとの接続が切断される)、サーバーが再起動され、クライアントによる再接続(およびサブスクライブ)が発生する可能性があります。これと比較して:

  1. サーバの起動 - >クライアント接続 - >クライアントの購読。
  2. サーバーはシャットダウンし、クライアントは登録解除しません。
  3. サーバの起動 - >クライアント接続 - >クライアントの購読(2回目)
  4. これは/ tripple/...メッセージを2回発生させます。

あなたの問題を再現するものはありますか?