upgrade method

Response upgrade(
  1. WebSocketHandler handler, {
  2. String? protocol,
})

Accepts the websocket upgrade for the current request.

Implementation

Response upgrade(WebSocketHandler handler, {String? protocol}) {
  if (_event.method != 'GET') {
    throw HTTPError(405, headers: Headers({'allow': 'GET'}));
  }

  if (!isSupported) {
    throw const HTTPError(
      501,
      body: 'WebSocket is not supported by this runtime.',
    );
  }

  final webSocket = _event.context.webSocket;
  if (webSocket == null || !webSocket.isUpgradeRequest) {
    throw const HTTPError(426, body: 'Upgrade Required');
  }

  return webSocket.accept(handler, protocol: protocol);
}