AS-Devs / signalr_flutter

A flutter plugin for .net SignalR client.
https://pub.dev/packages/signalr_flutter
MIT License
18 stars 29 forks source link

connection problem when opening app from background #32

Open AliEasy opened 3 years ago

AliEasy commented 3 years ago

Hi I found a issue in package (so far on ios, not tested on android) At first after initializing signalr every thing is fine. But by pressing home button and running app in background and then opening app from background, a connection problem occurs and connection status keep changing without any success connection and continues until app is killed.

Here is the log

image

AyonAB commented 3 years ago

I will take a look into it. But I can suggest you one thing. As SignalR won't stay connected in the background state, you should disconnect when leaving app and reconnect when coming back or resuming the app. This should be your connection flow I guess.

AliEasy commented 3 years ago

ok so this is my code

@override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.inactive ||
        state == AppLifecycleState.detached) {
      print('inactive');
      _signalR.stop();
      _signalR1.stop();
      return;
    }

    if(state == AppLifecycleState.paused){
      _signalR.stop();
      print('pause');
      _signalR1.stop();
    }

    if (state == AppLifecycleState.resumed) {
      print('resumed');
      await _signalR.connect();
      // _signalR1.reconnect();
      Future.delayed(const Duration(milliseconds: 6000), () {
        _signalR1.connect();
      });
    }
  }

at first signalr stops, but after connecting, the problem occurs

image

AliEasy commented 3 years ago

After a couple of hours looking for the problem I found sth interesting

The problem with the above answer is that Im using to separate signalrs in my application, and so after coming back to application from background I have to reconnect 2 signalrs Otherwise if I set up only one signalr in my app every thing works fine and all I have to do is to stop and resume signalr between background and foreground like this:

@override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.inactive ||
        state == AppLifecycleState.detached) {
      print('inactive');
      _signalR.stop();
      return;
    }

    if(state == AppLifecycleState.paused){
      print('pause');
      _signalR.stop();
    }

    if (state == AppLifecycleState.resumed) {
      print('resumed');
      await _signalR.connect();
    }
  }

This problem also occurs when initializing signalrs for the first time (when opening app for the first time), and my work around for that is to initialize my second signalr with a delay like below:

initializeInternalSignalR();
Future.delayed(const Duration(milliseconds: 6000), () {
    initializeAlarmSignalR();
});

Otherwise signalr wont Work

But after all in case of reconnecting signalrs after coming back from background, the Delay trick wouldnt work and signalrs wont connect as said in the question