WebSocketChannel constructor

  1. StreamChannel<List<int>> channel,
  2. {String? protocol,
  3. Duration? pingInterval,
  4. bool serverSide = true}

Creates a new WebSocket handling messaging across an existing channel.

This is a cross-platform constructor; it doesn't use either dart:io or dart:html. It's also HTTP-API-agnostic, which means that the initial WebSocket handshake must have already been completed on the socket before this is called.

protocol should be the protocol negotiated by this handshake, if any.

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 close 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 this is a WebSocket server, serverSide should be true (the default); if it's a client, serverSide should be false.


WebSocketChannel(StreamChannel<List<int>> channel,
    {String? protocol, Duration? pingInterval, bool serverSide = true})
    : _webSocket = WebSocketImpl.fromSocket(
          channel.stream, channel.sink, protocol, serverSide)
        ..pingInterval = pingInterval;