rikulo / socket.io-client-dart

socket.io-client-dart: Dartlang port of socket.io-client https://github.com/socketio/socket.io-client
https://quire.io
MIT License
675 stars 185 forks source link

Reconnection Not Working (Connection Timeout) #218

Open pranav-solutelabs opened 3 years ago

pranav-solutelabs commented 3 years ago

I am using Socket version 3 on server end and here I tried to use latest stable verion 1.0.1 also beta3.nullsafety.0 and 1.0.1 doesnt work and beta3 yields connection timeout error upon trying to reconnect . Kindly help

Here is my socket client class:

class SocketClient {
  final SocketAuthService socketAuthService;
  final PlayerRepository playerRepository;

  SocketClient({
    required this.socketAuthService,
    required this.playerRepository,
  }) {
    connect();
  }

  final IO.Socket socket = IO.io(
      FlavorConfig.instance!.values.socketBaseUrl,
      IO.OptionBuilder()
          .setTransports(['websocket', 'polling'])
          // .enableForceNew()
          .enableReconnection()
          .build()
      // 'secure': FlavorConfig.isProduction(),
      );

  Stream<Result<Exception, bool>> connect() {
    final streamSocket = StreamSocket<Result<Exception, bool>>();
    if (socket.connected) {
      streamSocket.addResponse(Result.success(true));
    }
    socket.onConnect((data) async {
      debugPrint('HF: onConnect Called');
      final authResult = await socketAuthService
          .login(
            playerFirebaseUid: FirebaseAuth.instance.currentUser!.uid,
            socketClient: this,
          )
          .first;
      authResult.when(
        success: (player) async {
          if (player != null) {
            await playerRepository.cachePlayerModel(player);
            streamSocket.addResponse(Result.success(true));
          }
        },
        failure: (ex) {
          streamSocket.addResponse(Result.failure(Exception("Auth Error")));
        },
      );
    });
    socket.onConnectError((data) {
      debugPrint('HF: onConnect Error: $data');
      streamSocket.addResponse(Result.failure(Exception("Connect Error")));
    });
    socket.onConnectTimeout((data) {
      debugPrint('HF: onConnect Timeout: $data');
      streamSocket.addResponse(Result.failure(Exception("Connect TimeOut")));
    });
    socket.onReconnect((data) => debugPrint('HF: onReConnect $data'));
    return streamSocket.getResponse.asBroadcastStream();
  }

  void dispose() {
    socket.destroy();
  }
}
jumperchen commented 3 years ago

FYI: the version info. - https://github.com/rikulo/socket.io-client-dart#version-info

pdivita commented 2 years ago

I have the same issue. Once disconnected I cannot reconnect anymore

RukshanJS commented 2 years ago

I have the same issue. Once disconnected I cannot reconnect anymore

Same here. Works as expected only on emulator (tested on android emulator).

On a physical device, SocketException is thrown rightaway after disconnecting the internet. Then can only create a new socket connection, and cannot reconnect to last socket connection.