handleClient method

Future<void> handleClient(
  1. WebSocketContext socket
)

Handles an incoming WebSocketContext.

Implementation

Future<void> handleClient(WebSocketContext socket) async {
  var origin = socket.request.headers?.value('origin');
  if (allowedOrigins.isNotEmpty && !allowedOrigins.contains(origin)) {
    throw AngelHttpException.forbidden(
        message:
            'WebSocket connections are not allowed from the origin "$origin".');
  }

  _clients.add(socket);
  await handleConnect(socket);

  _onConnection.add(socket);

  socket.request.container?.registerSingleton<WebSocketContext>(socket);

  socket.channel.stream.listen(
    (data) {
      _onData.add(data);
      handleData(socket, data);
    },
    onDone: () {
      _onDisconnect.add(socket);
      _clients.remove(socket);
    },
    onError: (e) {
      _onDisconnect.add(socket);
      _clients.remove(socket);
    },
    cancelOnError: true,
  );
}