Reputation: 2345
I have my own socket implementation that supports connection from regular tcp client. Now I would like to add websocket support in my server program. In that case I will need to support handshaking and message framing protocols that are supported by major web browsers. I was able to handle the handshaking part, but was stuck in dealing with the framing and un-framing of the messages. Is there any existing C or C++ library that handles the encoding and decoding of the websocket message frames, and supports the major websocket protocols used by the major web browsers?
Most of the current implementation that I found (i.e. libwebsocket, websocketpp, etc) implement their own server and client library, which means that I need to use their socket implementation. I don't want to do that because this will require me to modify a lot of things in my current program, and it is not an option for me. What I need is just a simple library that handles the encoding and decoding of the websocket frames (and/or also handle the handshaking part, but it is not compulsory).
Upvotes: 5
Views: 4743
Reputation: 21
You can use boost now to run asynchronous non-blocking ssl websocket connections. Check this example:
Instead of calling ios.run(); like in the example, use your own for loop(from your network code) and call ioc.poll();
The websocket connection will be handled completely in the background and pass events to a callback class. all of the events are asynch and non-blocking. and will run along side your network code perfect. there are also additional polling calls like ioc.poll_for(); to poll for a specific amount of time.
It would end up looking something like:
int main()
{
net::io_context ioc;
ssl::context ctx{ssl::context::tlsv12_client};
load_server_certificate(ctx);
// create the instance of the boost::asio websocket client
auto websock = std::make_shared<session>(ioc, ctx);
websock->run("gateway.discord.gg", "443", "");
// create a server from your own networking code;
auto server = MyLibrary->createSomeAsynchNonBlockingServer("3455", &my_librariies_event_handler);
while(MyLibrary->running)
{
if(MyLibrary->eventHandler()->proccessServer())
server->acceptClient(); // accept normal connection from your libraries server
ioc.poll(); // poll the boot::asio::io_context for any eventHandlers that have waiting events This function is non-blocking
}
}
Upvotes: 0
Reputation: 2750
Websocketpp library author here.
The frame processing and handshake processing code is completely separate from the socket/network code. Look at the processors folder of the policy-refactor branch. There is one for draft 76 (hybi_legacy) and one for RFC6455 (hybi/hybi_header). The frame processors read from an STL stream that you can fill via your own network code or from some other source.
Send me a PM on github if you have any more specific questions.
Upvotes: 5
Reputation: 8660
The websocketpp
library is nice designed and the frame handling classes are not mixed with socket ones. There is dependency on the BOOST and STL libraries. STL is not a problem and the BOOST dependency is quite easy to avoid. Just start from the websocket_frame.hpp file of the policy-refactor branch.
Upvotes: 5