Home > Back-end >  Flutter - Socket connection keep timed out and reconnecting by itself
Flutter - Socket connection keep timed out and reconnecting by itself

Time:09-30

The native socket server is fine. I've connected to it from other clients as well without any issues, even managed to transmit messages between clients.

And then I need to do the same from Flutter. I tried socket_io_client.

  Socket socket = io('http://192.168.1.2:5000', <String, dynamic>{
    'autoconnect' : false,
  });
  socket.onAny((String event, data){
    print([event, data]);
  });
  socket.connect();

Sadly socket.onAny heard nothing except timeout error with the event named connect_error. From server log, the client was shown as repeatedly connected to the server with new port number everytime, but without any signal of disconnection. I don't know how to keep it from timeout and even autoconnect : false setting was ignored.

UPDATE

Instead of fixing the car, I throw away the car into the cliff and buy a new car. I use flutter_io_socket instead.

  Socket socket = io('http://192.168.1.2:5000', <String, dynamic>{
    'autoconnect' : false,
  });
  socket.onConnect((p) => print(['CONNECT', p]));
  socket.onDisconnect((p) => print(['DISCONNECT', p]));
  socket.onError((p) => print(['ERROR', p]));
  socket.onConnecting((p) => print(['CONNECTING', p]));
  socket.onConnectError((p) => print(['CONNECT ERROR', p]));
  socket.onConnectTimeout((p) => print(['TIMEOUT', p]));
  socket.on('connect_error', (p) => print(['CONNECT ERROR', p]));
  socket.connect();

And now I got clearer vision.

I/flutter (28975): attempting reconnect

I/flutter (28975): readyState closed

I/flutter (28975): opening http://192.168.1:2:5000

I/flutter (28975): creating transport "polling"

I/flutter (28975): setting transport websocket

I/flutter (28975): connect attempt will timeout after 20000

I/flutter (28975): connect attempt timed out after 20000

I/flutter (28975): socket close with reason: "forced close"

I/flutter (28975): socket closing - telling transport to close

I/flutter (28975): connect_error I/flutter (28975): cleanup

I/flutter (28975): [CONNECT ERROR, timeout]

I/flutter (28975): reconnect attempt error

I/flutter (28975): will wait %dms before reconnect attempt 5000

I/flutter (28975): [TIMEOUT, 20000]

Yes. Except for onError, onConnectError, and on('connect_error', other listeners heard nothing.

Greatly appreciate the help thank you.

CodePudding user response:

The plugins are not for standard socket or at least they're no longer useful for that. Just use provided Socket.

  • Related