Mosquitto:发布的mqtt消息未传送到websockets客户端

piok6c0g  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(285)

我在Windows上运行mosquito,并将其配置为在端口1883上作为mqtt工作,在端口9001上作为websockets工作。我以为我可以从mqtt客户端发布消息,并在websockets客户端上接收消息,但似乎并非如此。
我可以成功地将消息从一个mqtt客户端传递到另一个mqtt客户端,也可以从一个websockets客户端传递到另一个websockets客户端。总而言之,这两个协议之间似乎有一条不可逾越的边界。
这是Mosquitto的限制吗?我忘记在conf文件中设置一些东西了吗?
注意:我用-v选项调用Mosquitto,所以它的日志很冗长。我注意到它实际上记录了websockets客户端的连接和消息,而不是mqtt客户端,尽管在mqtt客户端,关于连接和发布的一切看起来都很好(我使用MQTTNet客户端发布,在mqtt或websockets模式下,PAHO javascript客户端在websockets模式下,用于订阅/接收)。
禁用或启用防火墙没有任何区别。
下面是conf文件中的相关部分:

listener 1883
protocol mqtt
listener 9001
protocol websockets
password_file mypasswordfile.pwd

这是我启动它时报告的内容:

prompt> ./mosquitto -c myconfig.conf -v
2022-10-15 20:25:53: mosquitto version 2.0.15 starting
2022-10-15 20:25:53: Config loaded from myconfig.conf.
2022-10-15 20:25:53: Opening ipv6 listen socket on port 1883.
2022-10-15 20:25:53: Opening ipv4 listen socket on port 1883.
2022-10-15 20:25:53: Opening websockets listen socket on port 9001.
2022-10-15 20:25:53: mosquitto version 2.0.15 running

这是来自发布者的相关代码片段(但我也尝试过mosquito_pub实用程序):

var mqttFactory = new MqttFactory();
var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder().
    // WithTcpServer("localhost").                     // This does NOT work
    WithWebSocketServer("ws://localhost:9001/mqtt").   // This works
    WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311).
    WithClientId("publisher-client-id").
    WithCredentials("myusername", "mypassword").
    Build();
mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None).Wait();

var builder = new MqttApplicationMessageBuilder();
var applicationMessage = builder
    .WithTopic("mytopic")
    .WithPayload("mypayload")
    .Build();
mqttClient.PublishAsync(applicationMessage, CancellationToken.None).Wait();

下面是来自订阅者的代码片段:

mqttConnect() {
    this._mqtt = new Paho.MQTT.Client('localhost', 9001, '/mqtt', 'subscriber-client-id');
    this._mqtt.onConnectionLost = this.onMqttConnectionLost.bind(this);
    this._mqtt.onMessageArrived = this.onMqttMessageReceived.bind(this);
    const options = {
        mqttVersion: 4,
        userName: 'myusername',
        password: 'mypassword',
        onSuccess: this.onMqttConnected.bind(this),
        onFailure: this.onMqttFailedConnecting.bind(this)
    };
    this._mqtt.connect(options);
    ...
}
onMqttConnected() {
    console.log('Connected to MQTT broker');
    ...
    this._mqtt.subscribe('mytopic');
}
onMqttMessageReceived(message) {
    console.log('Received MQTT message; topic = ' + message.destinationName + ', payload = ' + message.payloadString);
    ...
}
nlejzf6q

nlejzf6q1#

正如在注解中讨论的那样,有2个示例正在运行(一个是Windows服务,一个是命令提示符)。
关闭后台服务解决了该问题。
(It不清楚为什么两个示例都能绑定到端口1883)

相关问题