socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

Socket not connect in Real Device ( Android & iOS both)

Open VimeshPolaris opened this issue 2 years ago • 6 comments

HERE IS MY FLUTTER DOCTOR : [✓] Flutter (Channel stable, 3.0.1, on macOS 11.6 20G165 darwin-x64, localeen-GB) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] VS Code (version 1.67.2) [✓] Connected device (5 available) [✓] HTTP Host Availability

//HERE IS MY CODE
class SocketInit {
  static IO.Socket? socket;

  static socketInit() async {
    socket = IO.io(
      ApiUtils.socketUrl,
      IO.OptionBuilder().setTransports(['websocket']).enableAutoConnect().setExtraHeaders({'foo': 'bar'}).enableForceNewConnection().build(),
    );
    socket!.connect();
    debugPrint(" socket!.connected --- ${socket!.connected.toString()}");
    socket!.onConnect((data) {
      debugPrint("Socket Connect Successfully! ");
    });
    socket!.on("1", (data) {
      debugPrint("socket.on ----------- $data");
      SocketCommentResponse commentResponse = SocketCommentResponse.fromJson(jsonDecode(jsonEncode(data)));
      sendBroadcast(BroadcastUtils.liveComment, extras: jsonDecode(jsonEncode(commentResponse)));
      debugPrint('sendBroadCast --- ${commentResponse.toJson().toString()}');
    });
    socket!.once("1", (data) {
      debugPrint('once ----------- $data');
    });
  }

  static disposeSocket() {
    if (socket != null && socket!.connected) {
      socket!.dispose();
    }
  }
}

VimeshPolaris avatar Jun 02 '22 08:06 VimeshPolaris

did you fix your problem ? i have same problem

MarwanCS avatar Jun 13 '22 20:06 MarwanCS

i have same problem too

renysh avatar Jun 15 '22 22:06 renysh

@renysh i fixed my problem by using 2.0.0-beta.4-nullsafety.0 version dont use version 1.0.2 socket_io_client: ^2.0.0-beta.4-nullsafety.0

MarwanCS avatar Jun 15 '22 23:06 MarwanCS

I can confirm that using the 2.0.0 pre release version fixed my problem when using "socket.io": "4.5.1",

whygee-dev avatar Jun 19 '22 00:06 whygee-dev

I can confirm that using the 2.0.0 pre release version fixed my problem when using "socket.io": "4.5.1",

HI, can you please confirm whether when internet connection is turned off and then on, does it connect to the existing last socket connection (on a real physical device)?

For me, on emulator, the socket reconnects when internet is back on (as expected), but on a physical device it just throws SocketException immediately after internet disconnects (No such exception when turn off internet in android emulator)

RukshanJS avatar Jul 01 '22 10:07 RukshanJS

I have same problem. I using socket_io_client: ^2.0.0-beta.4-nullsafety.0 and Flask-SocketIO 5.3.2 but it still can’t connect in Real Device.

L-Pisitpipattana avatar Apr 03 '23 06:04 L-Pisitpipattana