使用IP地址作为主机名时,Paho MQTT主机参数无效吗?

时间:2019-05-05 22:52:16

标签: javascript websocket mqtt mosquitto paho

修订:请注意,我现在正在使用IP地址10.0.0.15,发布到MQTT代理的设备是10.0.0.122。这仍然可以通过终端进行。

我认为我正在使用MQTT连接器。遇到以下帖子中所述的问题后,我已经前进了

Can't connect to localhost Mosquitto Broker with Javascript?

我现在看到以下错误。

         mqttws31.js:1585 Uncaught Error: AMQJS0013E Invalid argument 
         169.254.118.199 for host.
         at new M (mqttws31.js:1585)
         at startConnect (n.js:29)
         at HTMLInputElement.onclick ((index):107)

根据js文件,它指示匹配错误。我尝试将IP地址添加为wss://169.254.118.199,但这不能解决问题。你知道是什么原因造成的吗?

我尝试了以下

 wss://169.254.118.199
 ws://169.254.118.199
 wss://localhost
 tcp://169.254.118.199
 tcp://localhost

它们都产生相同的错误

这是错误指向mqttws31.js中的代码。

          if (arguments.length == 2) {
          // host: must be full ws:// uri
          // port: clientId
          clientId = port;
          uri = host;
          var match = uri.match(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))? 
          (\/.*)$/);
          if (match) {
          host = match[4]||match[2];
          port = parseInt(match[7]);
          path = match[8];
          } else {
          --> this is where error is pointing throw new Error(format(ERROR.INVALID_ARGUMENT,[host,"host"]));
          }
          } else {
          if (arguments.length == 3) {
            clientId = path;
            path = "/mqtt";
          }
          if (typeof port !== "number" || port < 0)
            throw new Error(format(ERROR.INVALID_TYPE, [typeof port, "port"]));
          if (typeof path !== "string")
            throw new Error(format(ERROR.INVALID_TYPE, [typeof path, "path"]));

          var ipv6AddSBracket = (host.indexOf(":") != -1 && host.slice(0,1) != "[" && 
          host.slice(-1) != "]");
          uri = "ws://"+(ipv6AddSBracket?"["+host+"]":host)+":"+port+path;
          }

编辑:在打印输出中,我在网页上看到以下内容已发送给Paho客户:

    Connecting to: 10.0.0.122 on port: 8083
    Using the following client value: clientID-64

我希望能够成功连接IP地址并获取MQTT有效载荷

1 个答案:

答案 0 :(得分:0)

要获取所看到的错误,您不能使用在其他问题中发布的代码:

clientID = "clientID-" + parseInt(Math.random() * 100);

// Fetch the hostname/IP address and port number from the form
host = document.getElementById("host").value;
port = document.getElementById("port").value;
// Print output for the user in the messages div

// Initialize new Paho client connection
 client = new Paho.MQTT.Client(host, Number(port), clientID);

仅当您仅将两个参数传递给Paho.MQTT.Client()构造函数而不是3时,才会发生该错误。在这种情况下,第一个参数被解释为完整的URI(例如ws://10.0.0.122:8083/mqtt),第二个参数被解释为完整的URI。客户ID。