signalr_client icon indicating copy to clipboard operation
signalr_client copied to clipboard

SignalR disconnects when put iOS app in background

Open nayanAubie opened this issue 1 year ago • 3 comments

  • 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 Reconnecting and Reconnected.
  • I've used an iPhone 11 with the release mode app.

nayanAubie avatar Feb 02 '24 04:02 nayanAubie

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

umairali4433 avatar Feb 14 '24 23:02 umairali4433

facing the same issue in android devices

os01ri avatar Feb 26 '24 12:02 os01ri

facing the same issue in android and iOS devices

xaldarof avatar Apr 24 '24 12:04 xaldarof