【问题标题】:mqtt mosca pahojs WebSocket connection to 'ws://localhost:1884/mqtt' failed/timeoutmqtt mosca pahojs WebSocket 连接到 'ws://localhost:1884/mqtt' 失败/超时
【发布时间】:2015-09-14 22:22:39
【问题描述】:

经纪人

var mosca = require('mosca')


var settings = {
  port: 1884
};

//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 whena  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) {
  if (packet.cmd === 'publish') {
    //Qui uso mongo DB 
    console.log('Published: ', packet.payload.toString('utf8'));
  }
});

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

client.html

<html>
  <head>
        <meta charset="utf-8" />
  </head>
  <body>
    <script src="./mqttws3.1.js"></script>
      <script>
  var client = new Paho.MQTT.Client( 'localhost', 1884, 'clientId');

  client.onConnectionLost = onConnectionLost;
  client.onMessageArrived = onMessageArrived;
  client.connect({onSuccess:onConnect});

  function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("/World");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message); 
};
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0)
  console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
  client.disconnect(); 
};  
      </script> 
    </body>
</html>

我经营经纪人

node broker

比我通过网络服务器调用 client.html 喜欢

http://localhost/client.html

过了一会儿就知道了

Firefox 无法与服务器建立连接 ws://localhost:1884/mqtt。 this.socket = new WebSocket(wsurl, ["mqtt"]);

Chrome:与“ws://localhost:1884/mqtt”的 WebSocket 连接失败: WebSocket 打开握手超时

我不知道该往哪个方向转 :(

你能帮帮我吗?

【问题讨论】:

    标签: node.js mqtt paho


    【解决方案1】:

    看看这个:

    https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets

    看起来您刚刚启动了一个普通的 MQTT 侦听器而不是 WS 侦听器。

    您需要在设置中添加一个 http 块:

    var settings = {
      http: {
        port: 1884,
        bundle: true,
        static: './'
      }
    };
    

    【讨论】:

    • 感谢我没有看到它。但是现在我遇到了一个奇怪的问题,如果我使用你的设置运行我有 events.js:85 throw er; // 未处理的“错误”事件 ^ 错误:在 Exports._errnoException (util.js:746:11) 处监听 EADDRINUSE._listen2 (net.js:1146:14) 在监听 (net.js:1172:10) 处net.js:1270:9 at dns.js:85:18 at process._tickCallback (node.js:355:11) at Function.Module.runMain (module.js:503:11) at startup (node.js: 129:16) 在 node.js:814:3
    • 你把port:1884去掉了吗?该错误意味着某些东西已经在使用端口 1884
    • 我已经尝试过像 gist.github.com/whisher/e61bfde612ba16e11986 这样的节点 v0.12.2
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多