title: Echo WebSocket in Dart
body: <p>I'm trying to make an "echo" WebSocket in Dart, but online I could find only outdated examples.</p>
  
  <p>This is my code at the moment:</p>
  
  <p>server:</p>
  
  <pre><code>import 'dart:io';
  import 'dart:convert';
  
  void main() async {
    final serverSocket = await ServerSocket.bind('127.0.0.1', 5600);
    await for (Socket socket in serverSocket) {
      socket.transform(utf8.decoder).listen((data) {
        print(data);
      });
    }
  }
  </code></pre>
  
  <p>client:</p>
  
  <pre><code>import 'dart:html';
  
  WebSocket socket;
  bool open = false;
  
  void main() {
    querySelector('#sendHello').onClick.listen(onClick);
    querySelector('#output').text = 'Your Dart app is running.';
  
    socket = WebSocket('ws://localhost:5600');
    socket.onMessage.listen(socketMessage);
    socket.onOpen.listen((_) =&gt; open = true);
  }
  
  void socketMessage(MessageEvent event){
    print('I recived: ${event.data}');
  }
  
  void onClick(MouseEvent event) {
    if (!open)
      print('Connection is not open!');
    else
      socket.send('Hello');
  }
  </code></pre>
  
  <p>The first text that is printed is:</p>
  
  <pre><code>GET / HTTP/1.1
  Host: localhost:5600
  Connection: Upgrade
  Pragma: no-cache
  Cache-Control: no-cache
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
  Upgrade: websocket
  Origin: http://localhost:63572
  Sec-WebSocket-Version: 13
  Accept-Encoding: gzip, deflate, br
  Accept-Language: en-US,en;q=0.9,it;q=0.8,lb;q=0.7
  Cookie: ....
  Sec-WebSocket-Key: ...
  Sec-WebSocket-Extensions: ...
  </code></pre>
  
  <p>then I'm unable to send/recive any more packets.</p>
  
  <p>How can I make the <code>ServerSocket</code> working properly?</p>