Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket disconnected when flutter page rerendered #407

Open
sdjalft opened this issue Jan 20, 2025 · 2 comments
Open

Socket disconnected when flutter page rerendered #407

sdjalft opened this issue Jan 20, 2025 · 2 comments

Comments

@sdjalft
Copy link

sdjalft commented Jan 20, 2025

My socket disconnected when my page rerendered.Here is my frontend code:

// create a socket connection
PinusSocketioClient(ip, port, onConnect: (client) async {
      final accessToken = await HttpHandler.getAccessToken();
      // emit a message to get result
      client.request([
        apiMap.CONNECTOR_entry,
        {
          'roomId': roomId,
          'userId': userId,
          'userName': userName,
          'userAvatar': userAvatar,
          'lat': 0.1,
          'lon': 0.1,
          'accessToken': accessToken
        },
        (data) {
          // getAllUserInfo
          _getAllUsers().then((usrs) {
            // a rerender events, when comment canceled,the rerender function disconnected my connection
            // updateUsers(usrs);
            completer.complete(true);
          });
        }
      ]);
    }, onDisconnect: (client) {
      // when rerendered, here comes.
      print('disconnected ${DateTime.now()}');
      connected = false;
      _currClient = null;
      client.disconnect();
      if (!completer.isCompleted) {
        completer.complete(false);
      }
    }, onError: (err, client) {
      print('error');
      connected = false;
      client.disconnect();
      if (completer.isCompleted) {
        completer.complete(false);
      }
    });

When the rerender function has been called, my node socketio process consoled 'ping timeout' immediatly.I know about socketio's ping/pong theory, but it happens every time when I rerendered and it is not caused by io's heartbeat.

Image

Then my frontend application closed because of 'transport close'.

I am very certain that my socket does not disposed by myself. Socket is singleton in my app and cannot be garbage collection.

I'm very curious about the bug in my app and I can't find a way to solve it 😭😭😭. I'll be truly and deeply grateful for an answer!

@sdjalft
Copy link
Author

sdjalft commented Jan 20, 2025

My socket.io-client-dart version is ^3.0.2 and my nodejs socketio version is 4.8.1

@sdjalft
Copy link
Author

sdjalft commented Jan 20, 2025

I tried emulator in Android Studio, and it doesn't disconnected when app rerendered. But when I push my route with go_router,socket disconnected again😭😭😭.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant