WebSocket Library

I want to access the WebSocket API using C ++ on Linux. I have seen different libraries (e.g. libwebsockets or websocketpp), but I'm not sure what to use. The only thing I need to do is connect to the API and get the data for the string. Therefore, I am looking for a very basic and simple solution, nothing complicated. Maybe someone already managed to get acquainted with the WebSocket library?

+6
source share
1 answer

For a high-level API, you can use ws_client in the cpprest library {it wraps websocketpp }.

Example application that runs against the echo server :

 #include <iostream> #include <cpprest/ws_client.h> using namespace std; using namespace web; using namespace web::websockets::client; int main() { websocket_client client; client.connect("ws://echo.websocket.org").wait(); websocket_outgoing_message out_msg; out_msg.set_utf8_message("test"); client.send(out_msg).wait(); client.receive().then([](websocket_incoming_message in_msg) { return in_msg.extract_string(); }).then([](string body) { cout << body << endl; // test }).wait(); client.close().wait(); return 0; } 

Here, the .wait() method is used to wait for tasks, but the code can be easily modified to perform I / O asynchronously.

+10
source

All Articles