2

I've got the following class member

boost::beast::websocket::stream<boost::beast::tcp_stream> ws_;

which gets instantiated on class c'tor using boost::asio::io_service as parameter.

I assume that all the websocket protocol is implemented on top of http so i was wondering if it's possible to extract the socket out of ws_ and use it to send regular http POST request ?

I've found an example that actually create the websocket out of simple tcp::socket here, so i guess it's doable, but how exactly ?

One good use-case for this for this approach is to fallback to simple HTTP requests (GET/POST) in case the websocket handshake failed (assuming it's supported by the server)

thanks

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • you want `https://www.boost.org/doc/libs/1_77_0/libs/beast/doc/html/beast/ref/boost__beast__websocket__stream/next_layer.html`, not sure whether fiddling with the underlying socket once you've created a websocket will cause unexpected results though – Alan Birtles Aug 30 '21 at 12:42
  • Sound like you really have a XY problem here. Why don't you simply create an ordinary http boost beast stream when the websocket creation failed? – Superlokkus Aug 30 '21 at 13:32
  • @Superlokkus, that's because i already did `boost::beast::get_lowest_layer(ws_).async_connect(...);` So i'd rather avoid re-connecting – Zohar81 Aug 30 '21 at 13:40
  • I would rather reconnect, because I assume you program logic has to be very different either way if you have to downgrade to regular http. Reconnecting shouldn't be very significant, HTTP1.0 even reconnects after every response. "Premature optimization is the root of all evil” – Superlokkus Aug 30 '21 at 13:51

0 Answers0