shamblett / mqtt5_client

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

Cannot add new events after calling close #72

Closed cyjaysong closed 6 months ago

cyjaysong commented 9 months ago

StateError:Bad state: Cannot add new events after calling close File "broadcast_stream_controller.dart", line 243, in _BroadcastStreamController.add File "event_bus.dart", line 61, in EventBus.fire File "mqtt_connection_handler_base.dart", line 162, in MqttConnectionHandlerBase.autoReconnect

shamblett commented 9 months ago

Looks like you've disconnected somehow, hence the client is now closed, without any more context of what you are doing that's all I can say really.

cyjaysong commented 9 months ago

Yes, due to network fluctuations, the connection has been disconnected and it is automatically reconnecting

shamblett commented 9 months ago

OK but you seem to be calling disconnect(), if you want to use the autoreconnect functionality you must set the auto reconnect flag and not call disconnect yourself, I'll need a full log to go any further.

cyjaysong commented 9 months ago

Yes, I called discount when the user account was logged out, and I think that after actively calling discount, there will be no automatic reconnection

shamblett commented 9 months ago

If you call disconnect() you must call connect() again before using the client.

cyjaysong commented 9 months ago

I think it executed reconnect after I called disconnect

shamblett commented 8 months ago

The client doesn't do this automatically, you have to call connect if you have called disconnect. I've done a quick update to the mqtt_server_client example to reconnect when the disconnect callback is triggered, output -

EXAMPLE::Mqtt5 client connecting....
EXAMPLE::OnConnected client callback - Client connection was successful
EXAMPLE::Mqtt5 server client connected, return code is success
EXAMPLE::Subscribing to the test/lol topic
EXAMPLE::Subscribing to the Dart/Mqtt5_client/testtopic topic
EXAMPLE::Sleeping....
EXAMPLE::Subscription confirmed for topic test/lol
EXAMPLE::Subscription confirmed for topic Dart/Mqtt5_client/testtopic
EXAMPLE::Publishing our topic now we are subscribed
EXAMPLE::Published notification:: topic is Dart/Mqtt5_client/testtopic, with Qos MqttQos.exactlyOnce
EXAMPLE::Change notification:: topic is <Dart/Mqtt5_client/testtopic>, payload is <-- Hello from mqtt5_client -->
EXAMPLE::Unsubscribing
EXAMPLE::Disconnecting
EXAMPLE::OnDisconnected client callback - Client disconnection
EXAMPLE::OnDisconnected callback is solicited, topic has been notified - this is correct
>>>>> Attempting reconnect >>>>
EXAMPLE::OnConnected client callback - Client connection was successful

You can see that we reconnect successfully when disconnect has been called. You seem not to be doing this, you sem to be destroying the client somehow.