MQTT-over-Websockets在远程服务器上运行

时间:2015-07-05 13:11:31

标签: node.js mqtt

我正在使用类似的部署 https://medium.com/@lelylan/how-to-build-an-high-availability-mqtt-cluster-for-the-internet-of-things-8011a06bd000

现在我想发送消息 将浏览器组成我的设备(现在我的localhost中是假的)所以 如果我喜欢

代理client.hmtl和client.js都在我的localhost

client.html

<html>
  <head>
        <meta charset="utf-8" />
  </head>
  <body>
  <script src="./browserMqtt.js"></script>
  <script>
  var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' };
  var msg  = 'devices/' + device.nickname + '/msg' ;
  var var client = mqtt.connect(
      { host: 'localhost', 
      port: 3000,
    username: device.nickname,  
      password: device.secret  });  
  client.subscribe(msg);
  client.on('message', function(topic, payload) {
      console.log('My message ',[topic, payload].join(": "));
      client.end();
  });
  client.publish(msg, "hello world client html !");
    </script> 
    </body>

</html>

client.js

var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' };



var msg  = 'devices/' + device.nickname + '/msg' ;

var mqtt = require('mqtt');

console.log(msg);


var client = mqtt.connect('mqtt://localhost:1883',{  username: device.nickname,  password: device.secret   });

client.subscribe(msg);

client.on('connect', function() {
    console.log('Client successfully connected');
    client.on('message', function(topic, message) {
        console.log('received from', topic, message);
        client.publish(msg, 'Client 1 is alive.. Test Ping msg! ' + Date());
        client.end();
    });
});

工作正常。

但是如果我在远程服务器上有代理和client.html 使用

将client.js留在我的localhost上
var device = { nickname: 'wbk0da8v9l2wewmi', secret: 'mysecret' };



var msg  = 'devices/' + device.nickname + '/msg' ;

var mqtt = require('mqtt');

console.log(msg);


var client = mqtt.connect('mqtt://204.151.191.103:1883',{  username: device.nickname,  password: device.secret   });

client.subscribe(msg);

client.on('connect', function() {
    console.log('Client successfully connected');
    client.on('message', function(topic, message) {
        console.log('received from', topic, message);
        client.publish(msg, 'Client 1 is alive.. Test Ping msg! ' + Date());
        client.end();
    });
});

它没有用。

我想知道问题是什么? 我做错了什么?

更新

当我说它没有工作时,我的意思是

我可以看到

  

客户端已成功连接

从控制台窗口(当我运行client.js时)

但是我无法看到客户端的任何消息

  

收到

(on message事件未被触发)

和浏览器端

  

我的留言

(on message事件未被触发)

当我在firebug控制台中运行client.html时,我已经

Firefox无法在ws://myserver.me:3000 /建立与服务器的连接。

1 个答案:

答案 0 :(得分:0)

在client.html上你是否还更改了主机:&#39; localhost&#39;,端口:3000用于远程主机?

您是否与另一个MQTT客户端(如MQTT.fx或mosquitto_pub)成功连接到204.151.191.103端口1883?