websocket method

HttpRoute websocket({
  1. required String path,
  2. required FutureOr<ServerUniverseWebSocketConnection> onWebSocket(),
})

Implementation

HttpRoute websocket({
  required String path,
  required FutureOr<ServerUniverseWebSocketConnection> Function() onWebSocket,
}) {
  return get(
    path,
    (HttpRequest req, HttpResponse res) async {
      final ServerUniverseWebSocketConnection serverUniverseWebSocketConnection = await onWebSocket();
      return WebSocketSession(
        onOpen: (webSocket) async {
          await serverUniverseWebSocketConnection.onOpen(webSocket, req, res);
        },
        onClose: (webSocket) async {
          await serverUniverseWebSocketConnection.onClose(webSocket, req, res);
        },
        onError: (webSocket, error) async {
          await serverUniverseWebSocketConnection.onError(error, webSocket, req, res);
        },
        onMessage: (webSocket, data) async {
          await serverUniverseWebSocketConnection.onMessage(data, webSocket, req, res);
        },
      );
    },
  );
}