我在Windows上运行mosquito,并将其配置为在端口1883上作为mqtt工作,在端口9001上作为websocket工作。我想我可以从mqtt客户端发布一条消息,然后在websockets客户端上接收,但事实并非如此
我可以成功地将消息从mqtt客户端传递到另一个mqtt客户端,也可以将消息从一个websockets客户端传递到另外一个websockets客户端。总而言之,这两个协议之间似乎有一条无法逾越的边界。
这是Mosquito的限制吗?我忘了在conf文件中设置一些内容了吗?
注意:我使用-v选项调用Mosquitto,所以它以冗长的方式进行日志记录。我注意到,它实际上为websockets客户端记录连接和消息,但不为mqtt客户端记录,尽管在mqtt客户端端,关于连接和发布的一切似乎都可以(我使用MQTTNet客户端以mqtt或websockets模式发布,使用PAHO javascript客户端以websocket模式订阅/接收(。
禁用或启用防火墙没有任何区别。
编辑:以下是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
这是来自发行商的相关代码片段(但我也尝试过mosquitto_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);
...
}
正如注释中所述,有两个实例正在运行(一个在Windows服务中,一个在命令提示符中(。
关闭后台服务解决了此问题。
(目前尚不清楚为什么这两个实例都能绑定到1883端口(