IoWebSocketSession.fromUpgradedSocket constructor

IoWebSocketSession.fromUpgradedSocket(
  1. Socket socket,
  2. Uri uri, {
  3. required bool serverSide,
  4. String? protocol,
  5. int sendBufferSizeLimit = defaultSendBufferSizeLimit,
  6. Duration? sendTimeLimit = defaultSendTimeLimit,
  7. int maxTextMessageSize = defaultMaxMessageSize,
  8. int maxBinaryMessageSize = defaultMaxMessageSize,
})

A session over socket, a connection on which the WebSocket handshake has just been done (HttpResponse.detachSocket on a server, HttpClientResponse.detachSocket on a client); uri is its address. The handshake answer may still be on its way in socket (written with add): what the session sends follows it.

serverSide tells which side this is (a server does not mask its frames), protocol is the subprotocol agreed on. Past sendBufferSizeLimit bytes waiting to be sent (-1: no limit), or when what was sent has not left within sendTimeLimit (null: no limit), the connection is dropped and closeStatus is CloseStatus.sessionNotReliable. A text message larger than maxTextMessageSize bytes (UTF-8), or a binary one larger than maxBinaryMessageSize bytes (-1: no limit), is dropped unread and the session is closed with 1009 (message too big). The sizes are counted from the frames before dart:io assembles the message, so a message in many small frames is stopped as early as one big frame. Until its final fragment, a message takes the memory of its payload, however many fragments (empty ones too) it comes in.

A control frame between the fragments of a message (RFC 6455 ยง5.4) is taken at once: a ping is answered with its own payload, and a close ends the session with its code, the unfinished message dropped; the message itself arrives whole. (dart:io's own WebSocket reads the control frame into the message's buffer: the message loses the fragments before it, and the pong carries them, more than the 125 bytes a control frame may have.)

Implementation

factory IoWebSocketSession.fromUpgradedSocket(
  io.Socket socket,
  Uri uri, {
  required bool serverSide,
  String? protocol,
  int sendBufferSizeLimit = defaultSendBufferSizeLimit,
  Duration? sendTimeLimit = defaultSendTimeLimit,
  int maxTextMessageSize = defaultMaxMessageSize,
  int maxBinaryMessageSize = defaultMaxMessageSize,
}) {
  final guard = _GuardedSocket(
    socket,
    serverSide: serverSide,
    maxText: maxTextMessageSize,
    maxBinary: maxBinaryMessageSize,
    sendBufferSizeLimit: sendBufferSizeLimit,
    sendTimeLimit: sendTimeLimit,
  );
  return IoWebSocketSession._guarded(
    io.WebSocket.fromUpgradedSocket(
      guard,
      protocol: protocol,
      serverSide: serverSide,
    ),
    uri,
    guard,
  );
}