【问题标题】:Paho MQTT host invalid argument when using an IP address as hostname?使用 IP 地址作为主机名时,Paho MQTT 主机参数无效?
【发布时间】:2019-05-05 22:52:16
【问题描述】:

REVISION:请注意我现在使用的 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 负载

【问题讨论】:

  • eth0 似乎有问题,当我通过可以连接的终端运行 Mosquitto 时,当我在此代码中运行时,它将连接到我的设备。设备IP其实是169.254.84.122,真的没有任何意义。此外,当我尝试在 eth0 上设置静态 ip 时,pi 不会接受它。我当时就被难住了。
  • 编辑代码以将您输入Paho.MQTT.Client(host, Number(port), clientID); 的内容准确地打印到控制台,并将其添加到问题中,以便我们可以准确地看到您传递给代码的内容。
  • @hardlib 我在原始问题的底部附近添加了一个 *EDIT,显示正在发送到 Paho.MQTT.Client(host,Number(port), clientID);
  • 我再次测试了,我现在解决了这个主机名错误,我现在一直有连接超时? mqttws31.js:977 WebSocket 连接到“ws://10.0.0.15:8083/mqtt”失败:连接建立错误:net::ERR_CONNECTION_TIMED_OUT

标签: javascript websocket mqtt mosquitto paho


【解决方案1】:

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

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

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

【讨论】:

  • 嘿,经过一番斗争,我找到了出错的地方,基本上,我通过 WiFi 连接到 pi 并使用我的 pi wlan0 192.168.1.200,但我试图访问通过 eth0 的 MQTT 和 Web 浏览器不喜欢它。所以我现在已经硬连线并通过 eth0 做所有事情,我可以连接,但是当我去订阅该主题时,它就挂在那里。我也试过这个在线 MQTT 测试器lazyengineers.com/mqtt-websocket,它做同样的事情,它会连接到代理,但在订阅主题时只是挂在那里。有什么想法吗?
猜你喜欢
  • 2014-09-15
  • 2013-01-29
  • 2014-12-19
  • 2014-12-02
  • 2015-06-26
  • 2014-04-12
  • 2014-11-11
  • 2021-01-18
  • 2011-03-12
相关资源
最近更新 更多