当新客户端订阅而不是最后一条消息时如何获取所有消息(mqtt,保留)
how get all messages when new client subscribe instead last one message (mqtt, retain)
当新客户端订阅而不是最后一条消息时如何获取所有消息(mqtt,保留)?
我有两个应用程序。我设置了保留、清理标志和 qos:1。我对两个应用程序使用了相同的代码。
示例:
- app_1 -> 用户写消息:你好 --> topic1
- app_1 -> 用户写消息:hello2 --> topic1
- app_2 -> test_user -> 连接到主题 1
现在 test_user 应该从用户 (app_1) [hello 和 hello2] 获取所有消息,但我只收到“hello2”
let client = mqtt.connect('http://127.0.0.1:9001', {
clientId: "front-client",
username: "u",
password: "p",
clean: false
});
client.on('connect', (topic) => {
client.subscribe(this.topic, {qos: 1}, (err) => {
if (!err) {
console.log('message sent by mqtt')
}
});
});
this.mqttClient = client;
//handle incoming messages
client.on('message', (topic, message, packet) => {
//
});
sendMsgByMqtt(message: string) {
this.mqttClient.publish(this.topic, message, {retain: true, qos: 1});
}
你没有
MQTT 不是消息队列系统,它是一个 Pub/Sub 系统。
MQTT 只会为当前处于离线状态的现有客户端排队消息。
新客户端无法查看以前发布到主题的消息。
唯一的例外是,如果在发布消息时设置了保留位,并且代理将仅保留发布到该主题且设置了保留位的最后一条消息。任何设置了该标志的新消息都将替换之前的消息。
当新客户端订阅而不是最后一条消息时如何获取所有消息(mqtt,保留)? 我有两个应用程序。我设置了保留、清理标志和 qos:1。我对两个应用程序使用了相同的代码。 示例:
- app_1 -> 用户写消息:你好 --> topic1
- app_1 -> 用户写消息:hello2 --> topic1
- app_2 -> test_user -> 连接到主题 1
现在 test_user 应该从用户 (app_1) [hello 和 hello2] 获取所有消息,但我只收到“hello2”
let client = mqtt.connect('http://127.0.0.1:9001', {
clientId: "front-client",
username: "u",
password: "p",
clean: false
});
client.on('connect', (topic) => {
client.subscribe(this.topic, {qos: 1}, (err) => {
if (!err) {
console.log('message sent by mqtt')
}
});
});
this.mqttClient = client;
//handle incoming messages
client.on('message', (topic, message, packet) => {
//
});
sendMsgByMqtt(message: string) {
this.mqttClient.publish(this.topic, message, {retain: true, qos: 1});
}
你没有
MQTT 不是消息队列系统,它是一个 Pub/Sub 系统。 MQTT 只会为当前处于离线状态的现有客户端排队消息。 新客户端无法查看以前发布到主题的消息。
唯一的例外是,如果在发布消息时设置了保留位,并且代理将仅保留发布到该主题且设置了保留位的最后一条消息。任何设置了该标志的新消息都将替换之前的消息。