shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Reconnect will calling repeatly when network are poor #75

Closed lucasjinreal closed 6 months ago

lucasjinreal commented 9 months ago

hi, I found when the network are poor, the mqtt reconnect will run repeatly fast, caused whole UI stoke, why

shamblett commented 9 months ago

If you are using the autoreconnect functionality it will run till it reconnects unless you stop it.

I cant comment on why this affects your ui.

lucasjinreal commented 9 months ago

shall make connect function be a async function?

Currently it happens when network are not stable, don't why

shamblett commented 9 months ago

If your network is so unstable you can't connect then you ate going to have trouble with MQTT. It makes no difference to the client whether its sync or async, if you can't connect you are stuck. You need to put the client as a whole on its own isolate maybe.

lucasjinreal commented 9 months ago

Is there any reconnect timeout try times or interval time params to set? Currently it will reonnect quickly when network unstable (note: not offline, if offline I can detect, but network not stable or slow I can not really detect this)

shamblett commented 9 months ago

You can change the amount of retries, see the API spec. You could monitor the connect callback, if this is not called after a time interval you could disconnect, back off and try connecting later.

lucasjinreal commented 9 months ago

@shamblett thanks.

I have set maxReconnectTimes to 3:

 _client =
          MqttServerClient.withPort(host, cid, 1883, maxConnectionAttempts: 3);
      // MqttServerClient.withPort("test.mosquitto.org", cid, 1883,
      // maxConnectionAttempts: 3);
      kLog(
          '!!!! ------------> [conn] connect with $host, $port, $username, $password, ${_clientId} $_client');

      // _client!.logging(on: true);
      _client!.logging(on: false);
      _client!.onConnected = onConnected;
      _client!.onDisconnected = onDisconnected;
      _client!.onUnsubscribed = onUnsubscribed;
      _client!.onSubscribed = onSubscribed;
      _client!.onSubscribeFail = onSubscribeFail;
      _client!.onAutoReconnect = onAutoReconnect;
      _client!.pongCallback = pong;
      _client!.keepAlivePeriod = 60 * 3;
      _client!.autoReconnect = true;
      _client!.useWebSocket = useWebsocket;

      final connMessage = MqttConnectMessage()
          .authenticateAs(username, password)
          .withSessionExpiryInterval(3600 * 24 * 14) // last for 7 days
          .keepAliveFor(120)
          // .withWillTopic('lastwills')
          // .withWillMessage('Will message')
          .withClientIdentifier(cid);
      // .withProtocolName("MQTT")
      // .withProtocolVersion(4)
      // .withSessionExpiryInterval(interval)
      // .withWillQos(MqttQos.atLeastOnce);
      _client!.connectionMessage = connMessage;

But, when the network are poor, the connect actually connects, but it still will reconnect, ( i have receive the connected welcome message).

Can u give me some suggestions, why I set max tries and reconnect, it will reconnect repeatly very quickly?

shamblett commented 9 months ago

Your using autoreconnect = true so post initial connection.if the connection is lost the client will try and reconnect automatically forever.

If you turn this off you can control when you reconnect after a disconnect using the onConnected/onDisconnected callbaclks.

lucasjinreal commented 9 months ago

Your using autoreconnect = true so post initial connection.if the connection is lost the client will try and reconnect automatically forever.

If you turn this off you can control when you reconnect after a disconnect using the onConnected/onDisconnected callbaclks.

thanks, how to turn it off? You mean do not auto reconnect? But I need auto reconnect, in case people offline it will auto reconnect,

but the problem is the reconnect interval are too quicky