|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
Orbit has built-in support for RFC 6455 WebSockets. Because the core engine is asynchronous, a single server instance can hold millions of concurrent WebSocket connections efficiently without exhausting OS threads.
Instead of app.get(), use app.ws() and pass a handler that accepts a WebSocketConnection&.
Each connection accepts messages of up to 16 MiB by default, measured after permessage-deflate decompression. A frame whose header announces a larger payload is refused before the payload is buffered, and the connection is closed with status 1009 (Message Too Big). Malformed frames close it with 1002.
Lower the limit inside the route handler, before any message arrives:
send() sends a text frame and send_binary() a binary one. Incoming binary messages go to on_binary_message() when it is set; otherwise on_message() receives both kinds, as it always has.
send(), send_binary() and close() are safe to call from any thread, e.g. when broadcasting from a worker.
Connections follow RFC 6455 and RFC 7692 (permessage-deflate):
1002 for an unmasked client frame, an RSV bit set without a negotiated extension, a reserved opcode, a bad fragment sequence, or an invalid close code.1007 for text, or a close reason, that is not valid UTF-8.no_context_takeover.CI runs the Autobahn TestSuite against examples/websocket_echo_server.cpp; see .github/workflows/autobahn.yml.
Global middleware (app.use), group middleware and route middleware run before the handshake is accepted, exactly as for HTTP routes. A middleware that stops the request (for example jwt_auth returning 401) aborts the upgrade.
Browsers do not apply CORS to WebSocket handshakes, so check Origin to prevent cross-site WebSocket hijacking:
EventRouter::attach() takes the same middleware list:
For app.ws() handlers, ws.handshake_request() gives the same request while the handler runs (and null afterwards).
Handshakes that are not GET, lack Connection: Upgrade, carry a malformed Sec-WebSocket-Key, or request a version other than 13 are answered with 400 Bad Request.
To build a chat server, you can store WebSocketConnection references or broadcast messages globally. The underlying sockets are non-blocking, making send() extremely fast.