signalr_client
signalr_client copied to clipboard
SignalR disconnects when put iOS app in background
- I followed the exact code mentioned in the example project.
- When I put the iOS app in the background for around 30 seconds, SignalR disconnects automatically and when I back the app in the foreground again, it shows the log of
ReconnectingandReconnected. - I've used an iPhone 11 with the release mode app.
i was facing the same issue no one helped so what i did was i just created listner for every 3 second if my hub is disconnected then do reconnect sample code: import 'dart:async'; import 'package:flutter/material.dart'; import 'package:signalr_core/signalr_core.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('HubConnection Status Checker'), ), body: Center( child: HubConnectionStatusChecker(), ), ), ); } }
class HubConnectionStatusChecker extends StatefulWidget { @override _HubConnectionStatusCheckerState createState() => _HubConnectionStatusCheckerState(); }
class _HubConnectionStatusCheckerState extends State<HubConnectionStatusChecker> { late HubConnection _hubConnection; late Timer _timer;
@override void initState() { super.initState();
// Initialize your HubConnection here
_hubConnection = HubConnectionBuilder()
.withUrl('your_hub_connection_url')
.build();
// Start checking the connection status every second
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
// Check the connection status
if (_hubConnection.state == HubConnectionState.Connected) {
print('HubConnection is connected');
} else {
print('HubConnection is not connected');
// Optionally, try to start the connection again
// _hubConnection.start();
}
});
}
@override void dispose() { // Cancel the timer and dispose the HubConnection _timer.cancel(); _hubConnection.stop(); super.dispose(); }
@override Widget build(BuildContext context) { return Text('Checking HubConnection status...'); } }
and it worked perfectly now i am not facing this issue
facing the same issue in android devices
facing the same issue in android and iOS devices