connect static method

Future<WebSocket?> connect(
  1. Uri uri, {
  2. Map<String, dynamic>? headers,
  3. Iterable<String>? protocols,
  4. TransportPlatform? transportPlatform,
})

Create a new WebSocket connection. The given uri must use the scheme ws or wss.

Specify the subprotocols the client is willing to speak via protocols.

Additional headers to be used in setting up the connection can be specified in headers. This only applies to server-side usage. See dart:io's WebSocket for more information.

Implementation

static Future<WebSocket?> connect(Uri uri,
    {Map<String, dynamic>? headers,
    Iterable<String>? protocols,
    TransportPlatform? transportPlatform}) async {
  // If a transport platform is not explicitly given, fallback to the globally
  // configured platform.
  transportPlatform ??= globalTransportPlatform;

  if (MockTransportsInternal.isInstalled) {
    // If transports are mocked, return a mock-aware StreamedRequest instance.
    // This mock-aware instance will be able to decide at the time of dispatch
    // whether or not the mock logic should handle the request.
    return MockAwareTransportPlatform.newWebSocket(transportPlatform, uri,
        headers: headers, protocols: protocols);
  } else if (transportPlatform != null) {
    // Otherwise, return a real instance using the given transport platform.
    return transportPlatform.newWebSocket(uri,
        headers: headers, protocols: protocols);
  } else {
    // If transports are not mocked and a transport platform is not available
    // (neither explicitly given nor configured globally), then we cannot
    // successfully construct a WebSocket.
    throw TransportPlatformMissing.webSocketFailed(uri);
  }
}