未通过WebSocket从mosquitto接收有效负载
我正在使用VPS,并通过MQTT将数据从Arduino发送到服务器 Mosquit成功地通过终端打印有效负载,但当我尝试通过网页实时打印时,什么也没发生 知道我已经在MOSQUITO conf中允许WebSocket,如果我运行:未通过WebSocket从mosquitto接收有效负载,websocket,vps,mosquitto,Websocket,Vps,Mosquitto,我正在使用VPS,并通过MQTT将数据从Arduino发送到服务器 Mosquit成功地通过终端打印有效负载,但当我尝试通过网页实时打印时,什么也没发生 知道我已经在MOSQUITO conf中允许WebSocket,如果我运行: sudo netstat -plnt 我得到: tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 13248/mosquitto tcp 0
sudo netstat -plnt
我得到:
tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN
13248/mosquitto
tcp 0 0 0.0.0.0:1884 0.0.0.0:* LISTEN
20169/mosquitto
tcp6 0 0 :::1883 :::* LISTEN 13248/mosquitto
我发送的主题是姓名:/pression,我使用的代码是:
<script>
var websocket="myserver.ovh.net";
var port= 1884;
var user="username";
var pass="password";
client = new Paho.MQTT.Client(websocket, port, "innovation");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
var options = {
useSSL: false,
userName: user,
password: pass,
onSuccess:onConnect,
onFailure:doFail
}
// connect the client
client.connect(options);
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a
message.
document.getElementById("connstatus").innerHTML = "Mqtt Connected";
console.log("Mqtt Connected");
client.subscribe("/pression");
}
function doFail(e){
console.log(e);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
document.getElementById("connstatus").innerHTML = "Mqtt Not Connected";
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("Pression is :");
document.getElementById("connstatus").innerHTML = message.payloadString;
console.log(message.payloadString);
}
</script>
我得到了压力值
如果我将网页打开几分钟,我会收到以下消息:
Mqtt Connected
test.html:76 onConnectionLost:AMQJS0008I Socket closed.
配置文件:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
#password_file /etc/mosquitto/passwd
#allow_anonymous false
listener 1884
protocol websockets
莫斯基托日志:
1557922249: Config loaded from /etc/mosquitto/mosquitto.conf.
1557922249: Opening websockets listen socket on port 1884.
1557922254: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
1557922279: Socket error on client innovation, disconnecting.
1557922279: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
1557922318: Socket error on client innovation, disconnecting.
1557922318: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
1557922346: Socket error on client innovation, disconnecting.
1557922346: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
1557922363: Socket error on client innovation, disconnecting.
1557922364: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
1557922463: Socket error on client innovation, disconnecting.
好的,这里的问题很可能是您在HTML中使用了固定的客户端id(
innovation
)
您只能有一个客户端与给定的客户端id连接,因此当具有相同id的新客户端连接时(例如,当您重新加载页面时),代理将断开最旧的客户端
尝试将连接线更改为类似以下内容:
var clientID = "innovation_" + new Date().getTime();
client = new Paho.MQTT.Client(websocket, port, clientID);
我刚做了,但我还是犯了同样的错误。1557924290:Config从/etc/mosquitto/mosquitto.conf加载。1557924290:打开端口1884上的WebSocket侦听套接字。1557924296:新客户端从xx.xx.11.163连接为innovation_1557924296241(c1,k60,u‘innovation’)。1557924315:客户端innovation_1557924296241上发生套接字错误,正在断开连接。浏览器和代理之间是否有防火墙?我如何检查?请阅读文档,并尝试将keepAlive设置为大约10秒,而不是默认的60秒Hanks hardillb,我尝试了所有方法,但均无效,可能是SSL问题?因为我已经用cloudMQTT尝试了我的js代码,它工作得非常好,但是当我使用自己的VPS时,我发现了这个问题,唯一的区别是在cloudMQTT中我使用了安全websocket。
var clientID = "innovation_" + new Date().getTime();
client = new Paho.MQTT.Client(websocket, port, clientID);