Javascript 使用IP地址作为主机名时Paho MQTT host参数无效?
修订版:请注意,我现在使用的是IP地址10.0.0.15,发布到MQTT代理的设备是10.0.0.122。这仍然是通过终端工作 我想我正在使用MQTT连接器。我在遇到了如下帖子所述的问题后,继续前进 我现在看到以下错误Javascript 使用IP地址作为主机名时Paho MQTT host参数无效?,javascript,websocket,mqtt,mosquitto,paho,Javascript,Websocket,Mqtt,Mosquitto,Paho,修订版:请注意,我现在使用的是IP地址10.0.0.15,发布到MQTT代理的设备是10.0.0.122。这仍然是通过终端工作 我想我正在使用MQTT连接器。我在遇到了如下帖子所述的问题后,继续前进 我现在看到以下错误 mqttws31.js:1585 Uncaught Error: AMQJS0013E Invalid argument 169.254.118.199 for host. at new M (mqttws31.js:15
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;
}
我希望成功连接到IP地址并获取MQTT有效负载。要获取您看到的错误,您不能使用您在其他问题中发布的代码:
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。eth0似乎有问题,当我通过我可以连接的终端运行Mosquitto时,当我运行此代码时,它将连接到我的设备。设备IP实际上是169.254.84.122,这真的没有任何意义。另外,当我试图在eth0上设置静态ip时,pi就是不接受它。编辑代码,将输入到Paho.MQTT.Client(主机、号码(端口)、clientID)中的内容准确地打印到控制台
并将其添加到问题中,这样我们就可以准确地看到您传递给代码的内容。@hardlib我在原始问题底部附近添加了一个*编辑,显示发送给Paho.MQTT.Client的内容(主机、编号(端口)、clientID);我已经再次测试,我现在正在克服这个主机名错误,我现在一直有一个连接超时?mqttws31.js:977 WebSocket到“ws://10.0.0.15:8083/mqtt”的连接失败:连接建立中出错:net::ERR_connection_TIMED_out嘿,所以经过一番斗争后,我找出了哪里出了问题,基本上,我是通过WiFi连接到pi,并使用我的pi wlan0 192.168.1.200,但是我试图通过eth0访问MQTT,而web浏览器不喜欢它。所以我现在是硬连线的,通过eth0做所有事情,我可以连接,但是当我订阅主题时,它就挂在那里了。我也尝试过使用这个在线MQTT测试仪,它做同样的事情,它将连接到代理,但在订阅主题时挂起。有什么想法吗?
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);