3

I have connected to my server as follows:

Socket socket = await Socket.connect('xxx', xxx)

But now I want to check if the socket is still connected. It would be even better if I have a listener that tells me when the connection is broken.

Thanks already! Leonard

LeonardvV
  • 53
  • 5
  • 1
    Socket's `listen` method lets you read the data and lets you set an `onDone` method that's called when the socket is closed. – Richard Heap Jun 28 '22 at 16:24

1 Answers1

2

Listen to onDone in its stream

socket.stream.listen(
        (dynamic message) {
          debugPrint('message $message');
        },
        onDone: () {
          debugPrint('socket closed');//if closed you will get it here
        },
        onError: (error) {
          debugPrint('error $error');
        },
      );
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30