wsHandler function

Future<Response> Function(Request) wsHandler(
  1. WsHandler handler, {
  2. int maxMessageSize = 1024 * 1024,
})

Connects Dartvel's structured message/room API to this same transport.

Implementation

Future<Response> Function(Request) wsHandler(
  WsHandler handler, {
  int maxMessageSize = 1024 * 1024,
}) => webSocketHandler((channel, _) {
  final connection = _Connection(channel as NativeWebSocketChannel);
  WsManager.instance.registerConnection(connection);
  () async {
    try {
      await for (final message in connection.messages) {
        await handler(connection, message);
      }
    } finally {
      WsManager.instance.unregisterConnection(connection.id);
      await connection.close();
    }
  }().catchError((Object _) {
    channel.sink.close(1011, 'Handler failed');
  });
}, maxMessageSize: maxMessageSize);