sefidgaran / signalr_client

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

Failed to complete negotiation with the server: TimeoutException after 0:00:02.000000: Future not completed Dart #34

Open ingvilow opened 2 years ago

ingvilow commented 2 years ago

Hi, thanks you for such great package.

I am struggiling with this error - SEVERE: 2022-08-16 16:01:01.915620: Failed to complete negotiation with the server: TimeoutException after 0:00:02.000000: Future not completed

If there any chance to prevent it? I get a big pile of data and I don't want my websocket connection to fail and I need to wait until it is all done. As I understand it is not a backend issue (or may be I am wrong, I have searched a lot about this problem but any solutions dosen't work in my case).

My code is like that:

 Future<List> fetchAllBugStuff() async {
    Logger.root.level = Level.ALL;
    Logger.root.onRecord.listen((LogRecord rec) {
      print('${rec.level.name}: ${rec.time}: ${rec.message}');
    });

    final transportProtLogger = Logger("SignalR - transport");

    Logger? logger;

    final httpConnectionOptions = HttpConnectionOptions(
      accessTokenFactory: () => SharedPreferenceService().loginWithToken(),
      headers: defaultHeaders,
      logger: transportProtLogger,
      logMessageContent: true,
    );

    final hubConnection = HubConnectionBuilder()
        .withUrl(
          'http://10.10/home',
          options: httpConnectionOptions,
        )
        .build();
    await hubConnection.start();
    List fixation = [];
    if (hubConnection.state == HubConnectionState.Connected) {
      await hubConnection
          .invoke('GetAllBigStuff')
          .then((value) => fixation = value as List);

    }
    hubConnection.keepAliveIntervalInMilliseconds = 10 * 60 * 60 * 1000;
    hubConnection.onclose(({error}) {
      Logger.root.level = Level.SHOUT;
      Logger.root.onRecord.listen((LogRecord rec) {
        print('${rec.level.name}: ${rec.error}: ${rec.message}');
      });
      logger?.finer(error);
    });
    // print(fixation);
    return fixation;
  }

Thanks you for your replying :)

v0fbu1vm commented 1 year ago

It's not an Issue. Just change requestTimeout inside HubConnectionBuilder.

PROGrand commented 1 year ago

Common problem is race condition, especially on flutter hotReload. Check that start is not called twice at the same time. For example, if your code is based on 'chatPageViewModel.dart' just remove openConnection from model constructor:

image