eclipse-paho / paho.mqtt.golang

Other
2.77k stars 534 forks source link

MQTT 连接失败: status can only transition to connecting from disconnected #670

Closed erroot closed 3 months ago

erroot commented 7 months ago
opts.SetConnectionLostHandler(func(client mqtt.Client, err error) {
    fmt.Println("连接丢失,尝试重新连接...:", err)
    s.isDisconnect = true
})
   //自动重连线程
   go func() {
    s.connect()
    time.Sleep(15 * time.Second)

    for {
        if !s.client.IsConnected() || !s.client.IsConnectionOpen() || s.isDisconnect {
            log.Printf("连接断开,尝试重新连接.IsConnected:%v,IsConnectionOpen:%v isDisconnect:%v\n",
                s.client.IsConnected(),
                s.client.IsConnectionOpen(),
                s.isDisconnect)

            s.client.Disconnect(100)
            time.Sleep(15000 * time.Millisecond)

            s.connect()
        }
        time.Sleep(15 * time.Second)
    }
}()

    func (s *MqttClientSverice) connect() {
        if token := s.client.Connect(); token.Wait() && token.Error() != nil {
            log.Println("MQTT 连接失败:", token.Error())
            return
        }
        s.isDisconnect = false
        log.Println("MQTT broker connected...")
        for _, topic := range s.config.Topics {
            if token := s.client.Subscribe(topic, 0, s.onMessageReceived); token.Wait() && token.Error() != nil {
                log.Println("Error subscribing to topic:", token.Error())
            } else {
                log.Println("MQTT broker Subscribed  topic:", topic)
            }
        }
    }

这样写的重连机制有问题:MQTT 连接失败: status can only transition to connecting from disconnected 库内部认为状态不对,但是进程重启是可以重连的

MattBrittan commented 7 months ago

Sorry - I only understand English.

Only moving from disconnected to connecting was a deliberate choice (if the status is not disconnected then something is still running so connecting is not a valid operation). However it's possible there is a bug/logic error somewhere, unfortunately I'll need more information (ideally an example that replicates the issue) in order to understand the issue you are facing. Note that with AutoReconnect operational (the default) there is no need for you to call Connect again.

Zhseek commented 5 months ago

Sorry - I only understand English.

Only moving from disconnected to connecting was a deliberate choice (if the status is not disconnected then something is still running so connecting is not a valid operation). However it's possible there is a bug/logic error somewhere, unfortunately I'll need more information (ideally an example that replicates the issue) in order to understand the issue you are facing. Note that with AutoReconnect operational (the default) there is no need for you to call Connect again.

I have a similar problem.
IsConnected() is false,but AutoReconnect is not working。 Logs of ConnectionLostHandler,ReconnectHandler,ConnectionAttemptHandler were not recorded. I call Disconnect,sleep 2 seconds,call Connect,response is ’status can only transition to connecting from disconnected‘。 Kill the program,restart it ok. Check the status of mosquitto service is running normally.

MattBrittan commented 5 months ago

@Zhseek

IsConnected() is false,but AutoReconnect is not working

IsConnected() does not tell you whether there is a connection to the broker, it's more of an indicator as to whether there is a current connection or the client is attempting to reestablish one (so if that's returning false then either the reconnect settings are false or Disconnect has been called).

call Disconnect,sleep 2 seconds,call Connect,response is ’status can only transition to connecting from disconnected

Two seconds might, or might not, be long enough - it depends on what handlers are running.

Unfortunately without an example that reproduces the issue, or, at minimum, logs, it's difficult to trace this kind of issue. The more info you provide the more likely it is that someone will put some effort into fixing it (I don't use this client much, so will only look into this myself if I can easily replicate the issue).

MattBrittan commented 3 months ago

I'm going to close this because I can't really take any action without more information.