C or C++ library for encoding and decoding websocket frames
Asked Answered
I

4

5

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).

Infighting answered 3/3, 2012 at 4:55 Comment(1)
how about porting from phpws ? the code looks pretty straightforward.Gobbet
B
5

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.

Brasher answered 3/3, 2012 at 10:45 Comment(0)
L
5

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.

Libbi answered 10/3, 2012 at 0:12 Comment(1)
Ask any more questions here, using comments, instead so other people can also learn from it.Latonia
B
1

Frame c-parser (without handshake) you may found there

Briseno answered 8/5, 2019 at 13:35 Comment(2)
Welcome to StackOverflow! Please provide a code sample and not just a reference for your answer. We have guidelines on How to write a good answer.Dyslexia
Do u know how to use the library or did you intentionally leave it out?Grandmother
K
0

You can use boost now to run asynchronous non-blocking ssl websocket connections. Check this example:

https://github.com/boostorg/beast/blob/develop/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp

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
    }        
  }
Kittrell answered 15/11, 2021 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.