close method

Future<bool> close({
  1. int code = WebSocketStatus.normalClosure,
  2. String reason = '',
})

Implementation

Future<bool> close(
    {int code = WebSocketStatus.normalClosure, String reason = ''}) async {
  if (!_connected) {
    return true;
  }
  if (_socket == null) {
    return true;
  }

  //Prevent Re-Connect
  _disconnect = true;
  _connected = false;

  _stopPing();

  bool result = false;
  runZonedGuarded(() async {
    //This should trigger callback for Done()
    //And set _connected to false before returning from close
    await _socket?.sink.close(code, reason);
    logger.i('Socket closed ' + reason);
    result = true;
  }, (error, stack) {
    onReceiveError(error);
  });

  _subscription?.cancel();
  _socket = null;
  return result;
}