socket.io-client-dart
socket.io-client-dart copied to clipboard
Always Reconnect
Flutter:
socket = IO.io('http://x.x.x:4047',OptionBuilder().setTransports(['websocket']).disableAutoConnect().build());
socket!.on("connect", (data) => print("connect"));
socket!.on("disconnect", (data) => print("disconnect"));
socket!.on("connect_error", (data) => print("connect_error: $data"));
socket!.on("connect_timeout", (data) => print("connect_timeout: $data"));
socket!.on("connecting", (data) => print("connecting: $data"));
socket!.on("error", (data) => print("error: $data"));
socket!.on("reconnect", (data) => print("reconnect: $data"));
socket!.on("reconnect_attempt", (data) => print("reconnect_attempt: $data"));
socket!.on("reconnect_failed", (data) => print("reconnect_failed: $data"));
socket!.on("reconnect_error", (data) => print("reconnect_error: $data"));
socket!.on("reconnecting", (data) => print("reconnecting: $data"));
socket!.on("ping", (data) => print("ping: $data"));
socket!.on("pong", (data) => print("pong: $data"));
socket!.connect();
Typescript:
const io = require("socket.io")();
io.attach(httpServer, { pingInterval: 10000, pingTimeout: 5000, transports : ["websocket"], serveClient : false });
io.on("connection", function(socket: any) { console.log(socket.id+" connected"); socket.on("message", (message: any) => { }); socket.on("disconnect", (reason: any) => { console.log(socket.id+" disconnect"); }); });
httpServer.listen(4047, () => { console.log('server running '); });
Log:
I/flutter (11614): connect I/flutter (11614): disconnect I/flutter (11614): reconnect_attempt: 1 I/flutter (11614): reconnecting: 1 I/flutter (11614): reconnect: 1 I/flutter (11614): connect I/flutter (11614): disconnect I/flutter (11614): reconnect_attempt: 1 I/flutter (11614): reconnecting: 1 I/flutter (11614): reconnect: 1 I/flutter (11614): connect
There is no problem connecting from another platform (For example angular cli is working), there is this problem with the flutter connection.
I had same issue, the server error was:
time="2021-04-01T01:18:54+09:00" level=warning msg="closed:client namespace disconnect"
time="2021-04-01T01:18:54+09:00" level=warning msg="meet error:read tcp 192.168.0.2:8080->192.168.0.2:60551: i/o timeout"
It takes about 30 seconds to reconnect, and when it does, the message is lost.
I don't know if my solution is useful.
My server is laravel-echo-server. The socket.io version currently supported is 2.3.0, but the socket_io_client version I use is 2.0.0-beta.3-nullsafety.0, and the server version corresponding to this version should be 3.x.
So I fixed socket_io_client to ^1.0.0-nullsafety.0 to solve this problem.
Ping pong works very well.
Anybody solve this issue?
disconnect function is triggering per 30 seconds and reconnect again.
I' m using 2.0.0-beta.3-nullsafety.0 version for flutter client.
EDIT : Issue solved. I am using a node.js server and the problem was on the server side. Socket.io version was "2.4.0". I updated to latest version. (4.0.1). Everything works fine now. Hope, it helps someone.
I don't know if my solution is useful.
My server is laravel-echo-server. The socket.io version currently supported is
2.3.0, but the socket_io_client version I use is2.0.0-beta.3-nullsafety.0, and the server version corresponding to this version should be 3.x.So I fixed socket_io_client to
^1.0.0-nullsafety.0to solve this problem.Ping pong works very well.
it work :)) thank you