attachTo method

void attachTo(
  1. HttpServer server,
  2. Map? options
)

Captures upgrade requests for a http.Server.

Implementation

void attachTo(HttpServer server, Map? options) {
  options = options ?? {};

  // cache and clean up listeners
  server.listen((event) async {
    var req = event;

    if (WebSocketTransformer.isUpgradeRequest(req) &&
        transports.contains('websocket')) {
      var socket = await WebSocketTransformer.upgrade(req);

      var socketConnect = SocketConnect.fromWebSocket(req, socket);

      socketConnect.dataset['options'] = options;

      handleUpgrade(socketConnect);

      return socketConnect.done;
    } else {
      var socketConnect = SocketConnect(req);
      socketConnect.dataset['options'] = options;
      handleRequest(socketConnect);
      return socketConnect.done;
    }
  });
}