我正在尝试手动连接到MQTT服务器,然后订阅多个主题。我可以手动连接,但不能用同一个MQTT服务连接多个主题。
Angular使用ngx-mqtt库
问题/问题:-当尝试连接到第二个主题时,第二个话题将附加到第一个主题。
请参阅下面的图片,它不起作用,我的MQTT服务器拒绝了我的调用,因为在第二个主题的一个请求中附加了两个主题,在下面给出的代码中已经尝试了多种方法。
请参阅下面的图片,它正在处理另一个旧代码。
ngx-mqtt的文档非常差,这会导致对mqtt提出问题。
在我下面的代码中尝试了两件事,即。
根据文档创建一个可观察的内容并订阅
在添加两个主题后创建了一个Observable,然后订阅它
mqttServiceOpts1: IMqttServiceOptions = {
connectOnCreate: false,
hostname: 'example-mqtt.ca',
port: 8090,
path: '/mqtt',
protocol: 'wss'
}
connetMqtt() {
return new Promise((resolve, reject) => {
this.getmqttDetails()
.subscribe((data) => {
console.log(data.port)
console.log(data.clientId)
console.log(data.broker)
this.mqttServiceOpts1.clientId = data.clientId
this.mqttService.connect(this.mqttServiceOpts1);
const responsePustatusName= data.rootTopic + '/pustatus/inbox/+/response';
const vacateRooutetopicName= data.rootTopic + '/vacateroute/wc/+/route/response';
console.log('Response Topic Name ' + responsePustatusName);
this.obs1$ = this.mqttService.observe(responsePustatusName)
this.obs1$ = this.mqttService.observe(vacateRooutetopicName)
this.subs2$ = this.obs1$.subscribe((message: IMqttMessage) => {
console.log('msg: ', message.payload.toString())
});
})
resolve(true)
})
}
创建了2个不同的可观测值
mqttServiceOpts1: IMqttServiceOptions = {
connectOnCreate: false,
hostname: 'exmp-mqtt.ca',
port: 8090,
path: '/mqtt',
protocol: 'wss'
}
connetMqtt() {
return new Promise((resolve, reject) => {
this.getmqttDetails()
.subscribe((data) => {
console.log(data.port)
console.log(data.clientId)
console.log(data.broker)
this.mqttServiceOpts1.clientId = data.clientId
this.mqttService.connect(this.mqttServiceOpts1);
const responsePustatusName= data.rootTopic + '/pustatus/inbox/+/response';
const vacateRooutetopicName= data.rootTopic + '/vacateroute/wc/+/route/response';
console.log('Response Topic Name ' + responsePustatusName);
this.subs1$ = this.mqttService.observe(responsePustatusName).subscribe((message: IMqttMessage) => {
console.log('msg: ', message)
});
this.subs2$ = this.mqttService.observe(vacateRooutetopicName).subscribe((message: IMqttMessage) => {
console.log('msg: ', message)
});
})
resolve(true)
})
}