Add websocket support#236
Conversation
|
Sorry for the lack on feedback and thanks for the support and for the code contribution. I think the main reason I cannot go with this change as is currently wired is that it uses a custom version/configuration of libmicrohttpd without making it optional. I think that needs to be solved before we can merge this change - many consumers of the library use libmicrohttpd as installed by their OSes and this would practically break their builds entirely. |
Thanks for the quick reply. |
|
Whilst I appreciate concerns for the existing user base, change is inevitable. It is called progress. Existing projects relying on libmicrohttpd would need to update / re-compile their code. Just like all the other open-source projects do when there are API breaks in their codebases and / or dependencies. I've been a faithful libmicrohttpd user for nearly 10 years. It is just great, the API is ergonomic, the |
Hi are there any plans to provide a C-only WebSocket example? My project uses FORTRAN and pure C with no C++ whatsoever. |
|
libmicrohttpd removed microhttpd_ws in September 2025 after release v1.0.2 … |
|
@LeSpocky that's really a pity. |
|
The upgrade handling is still in place. I guess you would just need to handle the socket you get with some other code. Will look into that later. |
|
That's great to hear. This is what I am already doing, handling the WebSocket messages (a TCP connection) in a custom C code after the initial upgrade. |
|
Here is some sample code: |
|
Thanks for this, @gre-42 — the outcome this PR was after (WebSocket support, per #20) shipped in libhttpserver 2.0.0. It's a from-scratch v2 implementation rather than this branch, so I'm noting it here in case it's useful to you. The public surface:
A ready-to-run echo example lives at class echo_handler : public httpserver::websocket_handler {
void on_open(httpserver::websocket_session& session) override {
session.send_text("Welcome to the echo server!");
}
void on_message(httpserver::websocket_session& session, std::string_view msg) override {
session.send_text(/* echo */ std::string(msg));
}
void on_close(httpserver::websocket_session& session, uint16_t code, const std::string& reason) override { /* ... */ }
};
// ws.register_ws_resource("/echo", std::make_unique<echo_handler>());Full docs are in the README's WebSocket support section. A couple of notes that connect back to your PR:
So the goal here is achieved in 2.0 — appreciate you pushing this forward back in 2021. |
|
Closing as superseded — WebSocket support shipped in 2.0.0 (see the previous comment for the API and example). Thanks again, @gre-42, for driving this. |
Issue or RFC Endorsed by Maintainers
Enable WebSockets #20.
Description of the Change
Added websocket support.
Alternate Designs
Extending the existing
http_resourceclass was considered, but rejected in favor of a new class and multiple inheritance.Possible Drawbacks
libmicrohttpserver does not yet compile the websocket-support by default. It must be done by building
libmicrohttpd/src/microhttpd_ws.Verification Process
Created and tested an example application
hello_world_websocket.cppunder Ubuntu 20.04.Release Notes
Added support for websockets.