sefidgaran / signalr_client

A Flutter SignalR Client for ASP.NET Core
https://pub.dev/packages/signalr_netcore
MIT License
71 stars 111 forks source link

SignalR disconnects when put iOS app in background #76

Open nayanAubie opened 6 months ago

nayanAubie commented 6 months ago
umairali4433 commented 6 months ago

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 { 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

os01ri commented 6 months ago

facing the same issue in android devices

xaldarof commented 4 months ago

facing the same issue in android and iOS devices