zaphoyd / websocketpp

C++ websocket client/server library
http://www.zaphoyd.com/websocketpp
Other
6.97k stars 1.97k forks source link

How to support both tls and non-tls? #960

Open iGwkang opened 3 years ago

iGwkang commented 3 years ago
if (url.find("wss://") == 0)
    client = tls_client
else if (url.find("ws://") == 0)
    client = no_tls_client
barsnick commented 3 years ago

This is a bit tricky due to the different underlying types of websocketpp::config::asio_client and websocketpp::config::asio_tls_client which cannot simply be configured. On the website or somewhere in the github issues, there is an example of how to achieve this by inheritance.

I have modified the debug_client example to support both with a "simple" inheritance wrapper, but you will note that it's still a bit rocky. This should be what you're looking for: https://github.com/barsnick/websocketpp/commit/3375a500cdee13ea8af271dd099e069c3476e9e0

This could be expanded to the other examples, and should probably become a pull request.

zaphoyd commented 3 years ago

The short answer here is that you will need two different endpoints (one that uses a TLS config and one that does not) and code to decide which endpoint to initiate the connection via.

The long answer, as @barsnick mentions, is that this is something that is clunky to do right now and the library probably should have a bundled wrapper that does so.