jamiewest / signalr_core

ASP.NET Core SignalR Dart Client
https://pub.dev/packages/signalr_core
MIT License
90 stars 62 forks source link

Execute function on reconnect #41

Closed zuboje closed 3 years ago

zuboje commented 3 years ago

This is not an issue but more like enhancement. When I use a function "withAutomaticReconnect()", it is a great feature and I get automatically reconnected to the hub. But when the device joins the hub, I send a set of commands to "group" devices. With automatic reconnect, I have to write a logic to keep track of my connection state and re-join a group on a signalR hub. It would be nice if I could provide to "withAutomaticReconnect()" a function to execute upon successful reconnect, that way I can write in a code "when you reconnect, send this to the hub to join your group to start receiving data". Hopefully, it would be on your road map down the road :)

dees91 commented 3 years ago

@zuboje HubConnection already have such callbacks:

void onclose(ClosedCallback callback) {
  if (callback != null) {
    _closedCallbacks.add(callback);
  }
}

/// Registers a handler that will be invoked when the connection starts reconnecting.
void onreconnecting(ReconnectingCallback callback) {
  if (callback != null) {
    _reconnectingCallbacks.add(callback);
  }
}

/// Registers a handler that will be invoked when the connection successfully reconnects.
void onreconnected(ReconnectedCallback callback) {
  if (callback != null) {
    _reconnectedCallbacks.add(callback);
  }
}

https://github.com/jamiewest/signalr_core/blob/main/lib/src/hub_connection.dart#L959

So I think you can use one of those in described scenario.

zuboje commented 3 years ago

@dees91 Thank you, I missed that!