gql-dart / gql

Libraries supporting GraphQL in Dart
MIT License
267 stars 121 forks source link

How to make reconnect TransportWebSocketLink(TransportWsClientOptions()) #450

Open DartFlutterWorld opened 6 months ago

DartFlutterWorld commented 6 months ago
final link = Link.from([
      TransportWebSocketLink(
        TransportWsClientOptions(
          socketMaker: WebSocketMaker.url(
            () => "wss://scada.logovaz.group/api/subs/",
          ),
          connectionParams: () {
            return {"authorization": "Bearer $token"};
          },
          lazy: true,
          keepAlive: const Duration(seconds: 50),
          retryAttempts: 20,
          retryWait: (retries) async {
            log("number of retries: $retries");
            await Future.delayed(const Duration(seconds: 5));
          },
          shouldRetry: (errOrCloseEvent) {
            return true;
          },
          isFatalConnectionProblem: (errOrCloseEvent) {
            return false;
          },
        ),
      ),
    ]);

After authorization, I receive a token that lasts 5 minutes. I subscribe over the socket and listen for data. After about 5 minutes I get an error that my token is out of date. I need to make sure that if I receive this error, I can re-request a new token and make a reconnection. And insert a new token into the socket.