upgradeToWebSocket method

  1. @override
Future<WebSocketSession> upgradeToWebSocket({
  1. String? protocol,
})
override

Accepts the WebSocket handshake and returns the open connection.

protocol is the sub-protocol the server picked among those the client listed in Sec-WebSocket-Protocol, or null for none. Throws a StateError when the request is not a handshake (isWebSocketUpgrade is false), and an ErrorResponseException when the server refuses the handshake's details (the web starter's server: a 400 for a missing, repeated or malformed Sec-WebSocket-Key, a 426 for a Sec-WebSocket-Version other than 13). After the upgrade the HTTP response must not be used any more. The WebSocket starter calls this for its endpoints; applications rarely do.

Implementation

@override
Future<WebSocketSession> upgradeToWebSocket({String? protocol}) async {
  if (!isWebSocketUpgrade) {
    throw StateError('Not a WebSocket handshake');
  }
  final pair = WebSocketSessionPair(
    uri.replace(scheme: isSecure ? 'wss' : 'ws'),
    protocol: protocol,
  );
  _client.complete(pair.client);
  return pair.server;
}