AcpWebSocketClientTransport constructor

AcpWebSocketClientTransport(
  1. Uri endpoint, {
  2. AcpWebSocketAdapter? adapter,
  3. List<String> protocols = const <String>[],
  4. AcpHttpHeaders headers = const AcpHttpHeaders(),
  5. AcpHttpCookiePolicy cookiePolicy = AcpHttpCookiePolicy.include,
  6. AcpAffinityCookieStore? cookieStore,
  7. int maximumFrameBytes = 16 * 1024 * 1024,
  8. int maximumJsonNestingDepth = 128,
  9. JsonRpcCodec codec = const JsonRpcCodec(),
  10. bool originValidator(
    1. Uri endpoint
    )?,
  11. AcpRemoteDiagnosticHandler? onDiagnostic,
})

Starts connecting to endpoint.

Native adapters support custom headers and caller-managed affinity cookies. Browser adapters use the browser cookie jar and reject custom handshake headers. originValidator is an application policy hook, not a replacement for server-side origin validation.

Implementation

AcpWebSocketClientTransport(
  this.endpoint, {
  AcpWebSocketAdapter? adapter,
  List<String> protocols = const <String>[],
  AcpHttpHeaders headers = const AcpHttpHeaders(),
  AcpHttpCookiePolicy cookiePolicy = AcpHttpCookiePolicy.include,
  AcpAffinityCookieStore? cookieStore,
  int maximumFrameBytes = 16 * 1024 * 1024,
  int maximumJsonNestingDepth = 128,
  JsonRpcCodec codec = const JsonRpcCodec(),
  bool Function(Uri endpoint)? originValidator,
  AcpRemoteDiagnosticHandler? onDiagnostic,
}) : _adapter = adapter ?? createPlatformWebSocketAdapter(),
     _ownsAdapter = adapter == null,
     _protocols = List<String>.unmodifiable(protocols),
     _headers = headers,
     _cookiePolicy = cookiePolicy,
     _cookieStore = cookieStore ?? AcpAffinityCookieStore(),
     _ownsCookieStore = cookieStore == null,
     _maximumFrameBytes = maximumFrameBytes,
     _maximumJsonNestingDepth = maximumJsonNestingDepth,
     _codec = codec,
     _onDiagnostic = onDiagnostic {
  if (endpoint.scheme != 'ws' && endpoint.scheme != 'wss') {
    throw ArgumentError.value(endpoint, 'endpoint', 'must use ws or wss');
  }
  if (maximumFrameBytes <= 0) {
    throw RangeError.value(
      maximumFrameBytes,
      'maximumFrameBytes',
      'must be positive',
    );
  }
  if (maximumJsonNestingDepth <= 0) {
    throw RangeError.value(
      maximumJsonNestingDepth,
      'maximumJsonNestingDepth',
      'must be positive',
    );
  }
  if (originValidator != null && !originValidator(endpoint)) {
    throw ArgumentError.value(
      endpoint,
      'endpoint',
      'rejected by origin policy',
    );
  }
  // The controller is an owned field and is closed by [close] or [_fail].
  // ignore: close_sinks
  late final StreamController<JsonRpcWireMessage> incoming;
  incoming = StreamController<JsonRpcWireMessage>(onCancel: _cancelReadable);
  _incoming = incoming;
  stream = AcpDuplexStream<JsonRpcWireMessage>(
    readable: incoming.stream,
    writable: AcpWritable<JsonRpcWireMessage>(
      write: _enqueueWrite,
      close: close,
    ),
  );
  _channelFuture = _connect();
  _channelFuture.ignore();
}