handleWebSocketUpgrade method

  1. @override
Future<void> handleWebSocketUpgrade(
  1. HttpRequest request
)
override

Upgrades an HttpRequest to a WebSocket connection and registers it.

Override this method to customize the WebSocket upgrade process, for example to inspect or modify the socket before registering.

Implementation

@override
Future<void> handleWebSocketUpgrade(HttpRequest request) async {
  // Select the best matching Swamp subprotocol during the WS handshake.
  request.response.statusCode = HttpStatus.switchingProtocols;
  final socket = await WebSocketTransformer.upgrade(
    request,
    protocolSelector: (requestedProtocols) {
      return requestedProtocols.firstWhereOrNull((p) {
        final v = parseSwampSubprotocol(p);
        return v != null && kSwampSupportedProtocols.contains(v);
      });
    },
  );
  final info = request.connectionInfo;
  final connectionInfo = NetworkerSocketInfo(
    Uri(host: info?.remoteAddress.address, port: info?.remotePort),
    socket,
  );
  final id = addClientConnection(connectionInfo);
  if (id == kAnyChannel) {
    socket.close();
    return;
  }
  handleWebSocketConnection(id, connectionInfo, socket);
}