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

How to get event on socket connected #209

Open
UmeshChakre opened this issue Feb 8, 2022 · 2 comments
Open

How to get event on socket connected #209

UmeshChakre opened this issue Feb 8, 2022 · 2 comments

Comments

@UmeshChakre
Copy link

I dont see any event when socket is connected to server or am i mistaken?

@Hwan-seok
Copy link

You can await for WebSocket.connect and pass it directly to IOWebSocketChannel(websocket) instead of using IOWebSocketChannel.connect factory method.

There are more details to me because I have further business requirements But the following is the core part.
I only used(tested) this on Android and IOS.

final webSocket = await WebSocket.connect(
  websocketServerUrl,
).timeout(wsConnectionTimeout);

webSocket
  ..pingInterval = wsPingInterval
  ..listen(
    wsReceiver.onServerEvent,
    onError: onError,
    onDone: onConnectionClosed,
    cancelOnError: true,
  );

webSocketChannel = IOWebSocketChannel(webSocket);

@itsji10dra
Copy link

Here is the right way to do it, use await _channel?.ready, if socket is not ready, it will throw exception.

try {
      _channel = IOWebSocketChannel.connect(Uri.parse(wss://something));
      await _channel?.ready;
      Log.printMessage('Socket Connected');

     // Send your message
     _channel?.sink.add('my-data');
    } catch (exception) {
       Log.printMessage('Socket Exception: $exception');
      _channel = null;
       // Reconnect
    }

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

3 participants