IOWebSocketChannel.connect constructor

IOWebSocketChannel.connect(dynamic url, { Iterable<String> protocols, Map<String, dynamic> headers, Duration pingInterval })

Creates a new WebSocket connection.

Connects to url using WebSocket.connect and returns a channel that can be used to communicate over the resulting socket. The url may be either a String or a Uri. The protocols and headers parameters are the same as WebSocket.connect.

pingInterval controls the interval for sending ping signals. If a ping message is not answered by a pong message from the peer, the WebSocket is assumed disconnected and the connection is closed with a goingAway code. When a ping signal is sent, the pong message must be received within pingInterval. It defaults to null, indicating that ping messages are disabled.

If there's an error connecting, the channel's stream emits a WebSocketChannelException wrapping that error and then closes.


factory IOWebSocketChannel.connect(url,
    {Iterable<String> protocols,
    Map<String, dynamic> headers,
    Duration pingInterval}) {
  var channel;
  var sinkCompleter = WebSocketSinkCompleter();
  var stream = StreamCompleter.fromFuture(
      WebSocket.connect(url.toString(), headers: headers).then((webSocket) {
    webSocket.pingInterval = pingInterval;
    channel._webSocket = webSocket;
    return webSocket;
  }).catchError((error) => throw WebSocketChannelException.from(error)));

  channel = IOWebSocketChannel._withoutSocket(stream, sinkCompleter.sink);
  return channel;