我试图用 mosquitto 设置一个新服务器。当我在 Ubuntu 18.04 上设置 mosquitto 时,FireFox 和 Chrome 都可以通过 websocket 连接到它。当我在 Ubuntu 20.04 上设置 mosquitto 时,只有 Chrome 可以通过 websocket 连接到它。FireFox 开发人员工具给出了错误
The connection to wss://domain.example.com:10002/mqtt was interrupted while the page was loading.
Object { invocationContext: undefined, errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined." }
问题:如何让 Ubuntu 20.04 上的 mosquitto 与 Ubuntu 18.04 的行为相同,或者让 FireFox 连接到 Ubuntu 20.04 上的 mosquitto?
重现错误的步骤
这是我用来在全新安装的 Ubuntu 18.04 或 Ubuntu 20.04 上安装 mosquitto 的脚本。我确保保持相同的服务器和 IP 地址,因此 VPS 实例不会在每次实验之间发生变化:
#!bin/bash
# The following sensitive information has been substituted: somewebsitethatIown.com, example.com, username, password, ports 10001, 10002, 10009
apt-get update -y;
mkdir -p /etc/ssl/star.example.com/
cd /etc/ssl/star.example.com/
wget https://somewebsitethatIown.com/ssls.tar
tar xvf ssls.tar
cd ~/
# MQTT
apt-get install -y build-essential python quilt python-setuptools python3 libssl-dev cmake libc-ares-dev uuid-dev daemon libwebsockets-dev;
# user required for mqtt process
adduser --system --shell /bin/bash --group --disabled-password --home /home/mosquitto mosquitto;
mkdir -p ~/mqtt
cd ~/mqtt
wget http://mosquitto.org/files/source/mosquitto-1.4.10.tar.gz
tar zxvf mosquitto-1.4.10.tar.gz
cd mosquitto-1.4.10/
sed -i "s|WITH_WEBSOCKETS:=no|WITH_WEBSOCKETS:=yes|g" config.mk
make
make install
cp mosquitto.conf /etc/mosquitto
ldconfig
touch /etc/mosquitto/passwd
mosquitto_passwd -b /etc/mosquitto/passwd username password
mv /etc/mosquitto/mosquitto.conf /etc/mosquitto/mosquitto.conf.back
cat <<EOT >> /etc/mosquitto/mosquitto.conf
port 10009 localhost
listener 10001
certfile /etc/ssl/example.com/star.example.com.full.crt
cafile /etc/ssl/example.com/star.example.com.ca-bundle
keyfile /etc/ssl/example.com/STAR_example_com_key.txt
listener 10002
protocol websockets
certfile /etc/ssl/example.com/star.example.com.full.crt
cafile /etc/ssl/example.com/star.example.com.ca-bundle
keyfile /etc/ssl/example.com/STAR_example_com_key.txt
allow_anonymous false
password_file /etc/mosquitto/passwd
EOT
cat <<EOT >> /etc/systemd/system/mosquitto.service
[Unit]
Description=Mosquitto
[Service]
Type=simple
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
[Install]
WantedBy=multi-user.target
EOT
systemctl enable mosquitto.service;
systemctl start mosquitto.service;
启动并运行后,我会转到另一台计算机/服务器并创建一个简单的反应项目,如下所示:
npx create-react-app mosq;
cd mosq;
npm install --save paho-mqtt;
rm src/App.js;
cat <<EOT >> src/App.js
import React from "react";
import { render } from "react-dom";
import Paho from "paho-mqtt";
const host = "domain.example.com;
const user = "username";
const pass = "password";
const topicWeb = "topic-web";
const port = 10002;
export default class App extends React.Component {
componentDidMount() {
try {
const clientId = "id"+Math.random();
const client = new Paho.Client(host, port, clientId);
client.onConnectionLost = (response) => {
console.log(
"MQTT Lost Connection: " +
(response.errorCode !== 0
? response.errorMessage + " code: " + response.errorCode
: "Unknown MQTT Error")
);
};
client.connect({
onSuccess:()=>{
console.log("MQTT Connected....");
client.subscribe(topicWeb)
},
onFailure:e=>console.log("MQTT Error:", e),
timeout:2,
useSSL:true,
userName:user,
password:pass
});
} catch (e) {
console.log(e);
}
}
render() {
return <h1>hello world</h1>;
}
}
EOT
npm start;
然后我使用FireFox或Chrome访问http://localhost:3000(或用服务器托管页面的IP地址替换localhost)。MQTT Connected...无论我是在 Ubuntu 18.04 还是 20.04 上运行我的 mosquitto bash 脚本,Chrome 的开发人员工具都会显示。FireFox 将显示MQTT Connected...我是否在 Ubuntu 18.04 上运行了 mosquitto bash 脚本,但The connection to wss://domain.example.com:10002/mqtt was interrupted while the page was loading.如果我在 Ubuntu 20.04 上运行了 mosquitto bash 脚本,则会显示错误消息。