websocket_client and websocket_client_config lifetimes

Nov 4, 2015 at 4:37 PM
Hi there!

I'm trying to implement a Websocket library that uses Casablanca's web sockets. In doing so I need to be able to hold a websocket_client member in a class so I can use it to connect, send, receive, etc. The problem is, when a websocket_client is constructed we need to pass it the websocket_client_config member at construction time. If we later need to modify the config to add a header, for example, I don't see a way to do this. I see a couple of possible solutions:
  1. Hold a websocket_client pointer in some form, so I only instantiate it once I know I have all the config I'll need to initiate a connect.
  2. Add an overload of websocket_client::config() that returns a non-const websocket_client_config such that I can chain calls: client.config().headers().add(L"Header", L"Value") for example.
  3. ???
Any thoughts/suggestions would be much appreciated.

Justin
Coordinator
Nov 4, 2015 at 4:49 PM
Hi Justin,

First, depending on what your particular use case is, you might want to look at the library we use to implement our websockets: websocketpp. If you are only using casablanca for websockets and not for our taskpool (pplx::task), then you will be likely best served by skipping our async wrappers.

Otherwise, you will probably need to wrap the websocket_client in a shared_ptr or unique_ptr and initialize whenever your config options are finalized. Inside the constructor for websocket_client, we perform some analysis on the config and store the results (including the headers) so adding an overload to modify the headers will be not be as trivial as having a non-const "getter" for the config object.
Nov 4, 2015 at 4:52 PM
Thanks for the help! Good to know that a simple overload wouldn't fix my problem. I'll try the shared/unique pointer route. We do use Casablanca for our http requests, and we use pplx as well, so I do want the whole package.

Cheers,
Justin