sendPing method

Future<Uint8List?> sendPing({
  1. Duration? timeout,
})

Send a ping message to keep the connection alive. The returning future will fail if no pong is received withing the given timeout. The default timeout is 5 seconds.

Implementation

Future<Uint8List?> sendPing({Duration? timeout}) async {
  if (_pingCompleter == null || _pingCompleter!.isCompleted) {
    _pingCompleter = Completer<Uint8List>();
    _send0(SocketHelper.getPing(isUpgradedProtocol));
    try {
      Uint8List pong = await _pingCompleter!.future
          .timeout(timeout ?? Duration(seconds: 5));
      return pong;
    } on TimeoutException {
      if (isOpen) {
        rethrow;
      }
      _pingCompleter!.complete();
      return null;
    }
  } else {
    throw Exception('Wait for the last ping to complete or to timeout');
  }
}