open method

  1. @override
Future<KletsoSocket> open(
  1. KletsoTransportRequest request
)
override

Connects and authenticates.

Implementation

@override
Future<KletsoSocket> open(KletsoTransportRequest request) async {
  _handshakes++;
  await _latency();
  if (_handshakes <= scenario.failHandshakes) {
    throw const KletsoNetworkException('simulated handshake failure');
  }
  if (scenario.rejectToken) {
    throw const KletsoAuthException('invalid token');
  }
  final session = _sessions[request.token];
  if (session == null) throw const KletsoAuthException('unknown token');
  if (session.expired || _expired) {
    throw const KletsoAuthException('token expired', expired: true);
  }
  final socket = _FakeSocket(this, session);
  _sockets.add(socket);
  if (request.conversationId != null) {
    socket._attach(request.conversationId!, request.after);
  } else {
    socket._readySeq = 0;
    socket._listenUnattached();
  }
  return socket;
}