diff --git a/src/main.rs b/src/main.rs index 17115e5..7186c40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,10 +182,6 @@ async fn run_mqtt(pool: SqlitePool, tx: broadcast::Sender) { let (client, mut eventloop) = AsyncClient::new(opts, 100); - client.subscribe("notify/#", QoS::AtMostOnce).await.unwrap(); - - info!("MQTT subscribed to notify/#"); - let pool_clone = pool.clone(); let tx_clone = tx.clone(); @@ -214,6 +210,11 @@ async fn run_mqtt(pool: SqlitePool, tx: broadcast::Sender) { } Ok(rumqttc::Event::Incoming(Packet::ConnAck(_))) => { info!("MQTT connected"); + // clean_session=true 会在断线后丢弃订阅,重连时必须重新订阅 + match client.subscribe("notify/#", QoS::AtMostOnce).await { + Ok(_) => info!("MQTT subscribed to notify/#"), + Err(e) => error!("MQTT subscribe failed: {e}"), + } } Ok(_) => {} Err(e) => {