reconnect method
Force a reconnection attempt. This will cancel any pending reconnect timer and immediately attempt to reconnect.
This is useful when you detect that connectivity has been restored (e.g., using connectivity_plus in Flutter) and want to reconnect immediately instead of waiting for the next scheduled retry.
Example:
// In Flutter with connectivity_plus
connectivity.onConnectivityChanged.listen((result) {
if (result != ConnectivityResult.none && transmit.isReconnecting) {
transmit.reconnect();
}
});
Implementation
Future<void> reconnect() async {
if (_isClosed) {
print('Transmit is closed, cannot reconnect');
return;
}
// Cancel any pending reconnect timer
_reconnectTimer?.cancel();
_reconnectTimer = null;
_nextRetryTime = null;
// If already connected, no need to reconnect
if (_status == TransmitStatus.connected) {
// return;
}
// Reset reconnect attempts to allow fresh reconnection
_reconnectAttempts = 0;
_isReconnecting = false;
// Force immediate connection
await _connect();
}