设置 mosquitto broker 监听两个端口?

Seting mosquitto broker to listen on two ports?

我目前正在使用用于 mqtt 交易的 mosquitto 代理在 c# 中构建应用程序,我可以通过 tcp(端口 1883)或 tls(端口 8883)连接应用程序。这是使用 conf 文件的端口设置完成的,但是程序会在侦听器值中设置的任何端口上崩溃,因此只有默认端口有效——我不知道我是否允许用户 select tcp或 tls,但我想让它正常工作。

编辑:

# Config file for mosquitto
user mosquitto

port 8883

cafile /home/ubuntu/mosquitto-certs/ca/cacert.pem

certfile /home/ubuntu/mosquitto-certs/ca/requests/webservercert.pem

keyfile /home/ubuntu/mosquitto-certs/ca/requests/webserverkey.pem

tls_version tlsv1

listener 1883

persistence true

log_dest stderr

log_type error

log_type warning

log_type notice

log_type information

connection_messages true

log_timestamp true

auth_plugin /etc/mosquitto/auth-plug.so

auth_opt_host localhost

auth_opt_port 3306

auth_opt_user ****

auth_opt_pass *****

auth_opt_backends mysql

auth_opt_dbname test

auth_opt_userquery SELECT pw FROM users WHERE username = '%s' LIMIT 1

auth_opt_superquery SELECT IFNULL(COUNT(*), 0) FROM users WHERE username = '%s' AND super = 1

auth_opt_aclquery SELECT topic FROM acls WHERE username = '%s'

auth_opt_superusers S*

如果我通过 TLS 登录,工作正常,但是如果我断开连接并尝试通过 TCP 登录,我会收到以下错误:

'System.IO.IOException' 类型的第一次机会异常发生在 System.dll 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException' 类型的第一次机会异常发生在 M2Mqtt.Net.dll {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}

如果我翻转端口和侦听器值,同样的事情。

要断开连接的代码:

if (_mqttClient != null && _mqttClient.IsConnected)
{
        _mqttClient.Disconnect();
        _mqttClient = null;

        SubscribeBtn.Enabled = false;
        UnSubBTN.Enabled = false;
        PublishBtn.Enabled = false;
        ConnectBtn.Enabled = true;
        UsernameTB.Enabled = true;
        PasswordTB.Enabled = true;

        DisplayBox.Items.Clear();
}

恐怕这听起来像是您的客户有问题,这似乎不是蚊子问题。我建议使用 mosquitto_sub 和 mosquitto_pub 客户端检查您的 mosquitto 设置。您可以将它们用于纯 TCP 和 TLS 连接,例如

mosquitto_sub -p 1883 -h <host> -t '$SYS/#' -u <username> -P <password>

或者对于 TLS:

mosquitto_sub -p 8883 -h <host> -t '$SYS/#' -u <username> -P <password> --cafile <ca certificate>

如果这些有效,您需要检查您的程序是否存在问题。

我实际上是通过在安全侦听器之后使用 SSL/TLS 的配置设置侦听器 1883 和侦听器 8883 来实现它的。

感谢经纪人@ralight 和@kartben 的回复。