4

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

all_by_grace
  • 2,315
  • 6
  • 37
  • 52
  • 1
    how about porting from [phpws](http://code.google.com/p/phpws/source/browse/phpws/websocket.framing.php) ? the code looks pretty straightforward. – Chikei Mar 03 '12 at 09:09

4 Answers4

6

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.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
zaphoyd
  • 2,642
  • 1
  • 16
  • 22
  • 5
    Ask any more questions here, using comments, instead so other people can also learn from it. –  Mar 10 '12 at 08:48
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.

megabyte1024
  • 8,482
  • 4
  • 30
  • 44
1

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

Bzick
  • 11
  • 1
  • 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](https://stackoverflow.com/help/how-to-answer). – Joseph Cho May 08 '19 at 13:38
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
    }        
  }
teksatan
  • 21
  • 5