2016-04-06 4 views
3

私はIOTに基づいてプロジェクトをやっています。だから私はcloudmqttnodejsサーバを接続する必要があります。nodejsとcloud mqttを接続する

app.js

// Create a MQTT Client 
var mqtt = require('mqtt'); 

// Create a client connection to CloudMQTT for live data 
var client = mqtt.connect('xxxxxxxxxxx', { 
    username: 'xxxxx', 
    password: 'xxxxxxx' 
}); 

client.on('connect', function() { // When connected 
    console.log("Connected to CloudMQTT"); 
    // Subscribe to the temperature 
    client.subscribe('Motion', function() { 
    // When a message arrives, do something with it 
    client.on('message', function(topic, message, packet) { 
     // ** Need to pass message out ** 
    }); 
    }); 

}); 

はその後、私のサーバーを開始しました。しかし、何も起こっていません(エラーメッセージも警告もありません)。

答えて

3

、キープアライブ、はprotocolVersionなど

app.js

var mqtt = require('mqtt'); 
var options = { 
    port: 15255, 
    host: 'mqtt://m11.cloudmqtt.com', 
    clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8), 
    username: 'xxxxxxxxxxxxxxxxxx', 
    password: 'xxxxxxxxxxxxxxxxxx', 
    keepalive: 60, 
    reconnectPeriod: 1000, 
    protocolId: 'MQIsdp', 
    protocolVersion: 3, 
    clean: true, 
    encoding: 'utf8' 
}; 
var client = mqtt.connect('mqtt://m11.cloudmqtt.com', options); 
client.on('connect', function() { // When connected 
    console.log('connected'); 
    // subscribe to a topic 
    client.subscribe('topic1/#', function() { 
     // when a message arrives, do something with it 
     client.on('message', function(topic, message, packet) { 
      console.log("Received '" + message + "' on '" + topic + "'"); 
     }); 
    }); 

    // publish a message to a topic 
    client.publish('topic1/#', 'my message', function() { 
     console.log("Message is published"); 
     client.end(); // Close the connection when published 
    }); 
}); 
2

これは、接続がうまくいかない、これを追加しようも可能だと私はあなたが何かわかりますと思う:

client.on('error', function(err) { 
    console.log(err); 
}); 
+0

あなたの素早い応答に感謝します。私はこのコード行を追加しましたが、同じことが再び起こっています。 – Muhsin

0

をあなたのMQTTサーバーをのconfig /スタート場所にIPとポートのオープンを確認してください。これらの2つがウイルス対策のファイアウォールまたはサーバーのファイアウォールによってブロックされる可能性があります。クラウドmqttに接続する前に、これらのサーバー設定を確認してください。

activemq.xml and jetty.xml 

    name="openwire" uri="tcp://0.0.0.0:61616 (default) 
    name="amqp" uri="amqp://0.0.0.0:5672 (default) 
    name="stomp" uri="stomp://0.0.0.0:61613 (default) 
    name="mqtt" uri="mqtt://0.0.0.0:1883 (default) (Android and Ios team) 
    name="ws" uri="ws://0.0.0.0:61614 (default) (Php team) 

アクセスURLのMQTT:http://127.0.0.1:8172/admin/

Un:admin (default) 
    Ps:admin (default) 

そして、あなたはのNode.jsを使用してアプリケーションを構築している場合場合はモスカ(http://www.mosca.io

」はありますIoTをコア に見てご利用くださいPythonを使用してアプリケーションを構築する場合、hbmqtt(https://github.com/beerfactory/hbmqtt

開発者エンドコード

if (!window.WebSocket) { 
    $("#connect").html("\ 

      <p>\ 
      Your browser does not support WebSockets. This example will not work properly.<br>\ 
      Please use a Web Browser with WebSockets support (WebKit or Google Chrome).\ 
      </p>\ 
     "); 
} else { 
    function subscribeClient() { 
     var currentDate = new Date(); 
     var mqtt_clientId = CURRENT_USER_ID + "-" + currentDate.getTime();  
     var mqtt_host = '00.000.00.000'; 
     var mqtt_port = '00000'; 
     var mqtt_user = 'xxxxx'; 
     var mqtt_password = 'xxxxx'; 
     $("#connect_clientId").val("example-" + (Math.floor(Math.random() * 100000))); 
     var timeout = 3000/2; 
     var keepAliveInterval = 3000; 
     var maxMqqtConnectCount = 80; 
     client = new Messaging.Client(mqtt_host, Number(mqtt_port), mqtt_clientId); 
     client.onConnect = onConnect; 
     client.onMessageArrived = onMessageArrived; 
     client.onConnectionLost = onConnectionLost; 
     // the client is notified when it is connected to the server. 
     var onConnect = function (frame) { 
      console.log('mqqt connected.'); 
      $("#chat_conn").val(1); 
      $(".offline-alert").css("display", "none"); 
      // connecting client 
      client.subscribe(topicId); 
      //subscribing to multiple groups 
      var groups = JSON.parse($("#current_user_groups").val()); 
      $(groups).each(function (index, element) { 
       var group_id = element["GroupID"]; 
       var group_topic_id = getGroupTopicId(group_id); 
       client.subscribe(group_topic_id); 
      }); 

     }; 

     function disconnectClient() { 
      client.disconnect(); 
      $(".offline-alert").css("display", "block"); 
      $("#chat_conn").val(0); 
     } 

     function onFailure(failure) { 
      console.log('mqqt connectinn failed'); 
      $("#chat_conn").val(0); 
      $(".offline-alert").css("display", "block"); 
      connectMqtt(); 
     } 

     function onConnectionLost(responseObject) { 
      console.log('mqqt connectinn lost'); 
      $("#chat_conn").val(0); 
      $(".offline-alert").css("display", "block"); 
      connectMqtt(); 
     } 

     function onMessageArrived(message) { 
      if (!message) { 
       return false; 
      } 
      console.log(message.payloadString); 

      } 


     function connectMqtt() 
     { 
      var currentConnectionCount = getMqqtConnectionLostCount(); 
      if (currentConnectionCount <= maxMqqtConnectCount) 
      { 
       setMqqtConnectionLostCount(); 
       console.log('connecting mqqt ' + getMqqtConnectionLostCount() + ' times.'); 
       client.connect({ 
        timeout: timeout, //seconds 
        keepAliveInterval: keepAliveInterval, 
        userName: mqtt_user, 
        useSSL: false, // for secure connection on https #added by Virendra Yadav ver1.1 on 2015-02-03 to set MQTT SSL use setting 
        password: mqtt_password, 
        onSuccess: onConnect, 
        onFailure: onFailure, 
       }); 
      } else 
      { 

       console.log('mqqt unable to connect more than ' + maxMqqtConnectCount + ' times.'); 
       window.location.reload(); 
      } 
     } 
     connectMqtt(); 

     function setMqqtConnectionLostCount() 
     { 
      var currentConnectionLostCount = getMqqtConnectionLostCount(); 
      currentConnectionLostCount = parseInt(currentConnectionLostCount); 
      currentConnectionLostCount = currentConnectionLostCount + 1; 
      $('#mqqtReconnectConnectionCount').val(currentConnectionLostCount); 
     } 
     function getMqqtConnectionLostCount() 
     { 
      var countVal = $('#mqqtReconnectConnectionCount').val(); 
      return countVal; 
     } 
    } 
+0

あなたの素早い応答をありがとう。このファイルはどこにありますか? – Muhsin

+1

あなたのconfフォルダ.. \ apache-activemq-5.11.1 \ confをチェックし、ファイアウォールもインバウンドとアウトバウンド用のオープンポートまたはIPを設定してください –

+0

回答はすべてコードブロックにする必要はありません、再フォーマットして読めるようにしてください – hardillb

1

私はCLOUD MQTTとNodeJsのインターフェースを実装しています。パスワードはバッファであることを

cloudmqttnodejsサーバがのclientIdなどの追加パラメータを与えることによって接続されている
 

    
    var mqtt = require('mqtt'),url = require('url') 
    var client = mqtt.createClient(PORTNO,"m10.cloudmqtt.com", 
    { 
     username: "xxxxxxxxx", 
     password: "xxxxxxxxx" 
    }); 

    client.on('connect',function() 
    { 
     client.publish("Hello",function() 
     { 
      client.end(); 
     }) 
    }) 
 

0

注意とオプションの任意の属性を渡すことができます。

var options = { username: 'strUserName', 
       password: new Buffer('strPassword')}; 

var client = mqtt.connect('mqtt://m11.cloudmqtt.com', options); 
関連する問題