dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
423 stars 112 forks source link

Unable to communicate regardless of successful connection #135

Open bdevay opened 3 years ago

bdevay commented 3 years ago

Hi,

I've created a quite simple code for testing the capabilities of the websocket via Flutter web. On the server-side, I'm using the SimpleWebsocketServer python module. I can connect to the server but I can't send anything there, it doesn't arrive but there is no error on any side.

Here is my client code (Flutter web)

start() async {
    HtmlWebSocketChannel channel;
    while (true) {
      try {
        if (channel == null || channel.closeCode != null) {
          channel = HtmlWebSocketChannel.connect("ws://localhost:8000");
          channel.stream.listen((message) {
            print(message);
          });
          channel.stream.handleError((error) {
            print(error);
          });
        }
        channel.sink.add("Test message");
      } catch (e) {
        print(e);
      }
    }
  }

Here is my server code (Python):

from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket

class SimpleEcho(WebSocket):

    def handleMessage(self):
        # echo message back to client
        print(self.data)
        self.sendMessage(self.data)

    def handleConnected(self):
        print(self.address, 'connected')

    def handleClose(self):
        print(self.address, 'closed')

server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()

Here you can find the Wireshark log: https://drive.google.com/file/d/1k_pmXIkBEhojl8FYj6X29ZH2pWx9xcFz/view?usp=sharing

And here is a screenshot of the relevant part of the Wireshark log: image

Is there any idea why it doesn't work?

bdevay commented 3 years ago

I don't know the reason but when I have moved the code from my custom class to a StatefulWidget, it started to work seamlessly.