1

I am trying to detect when a client has disconnected from the server. I thought that this was the correct way of doing it, but "someone disconnected" is never printed to the console.

import 'dart:io';

class VersusServer {
  Future<void> run() async {
    final PORT = 7171;

    var server = await HttpServer.bind(InternetAddress.loopbackIPv4, PORT);
    server.forEach(_handleRequest);
    print('Server listening on port $PORT.');
  }

  void _handleRequest(HttpRequest request) async {
    if (request.uri.path == '/ws') {
      var socket = await WebSocketTransformer.upgrade(request);
      print('someone connected');

      // MARK: this is the part I am trying to listen to disconnect
      socket.done.then((value) => print('someone disconnected'));
      socket.listen((message) {
        print(message);
      });
    }
  }
}

Is there something wrong in my code? Thanks in advance!

Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25

0 Answers0