How can I use a websocket with c++ -


i'm new c++ , i'm wondering how can use web sockets it, i've used web sockets in nodejs , javascript , want go on use them c++.

sockets not part of c++ standard library yet. boost has boost.asio, cross platform library talking tcp/ip , udp among other things. there's great open source library called beast handles not websocket http well, , built on top of boost.asio. here's library home page: http://vinniefalco.github.io/

here's complete, compiling example program talks websocket:

#include <beast/to_string.hpp> #include <beast/websocket.hpp> #include <boost/asio.hpp> #include <iostream> #include <string>  int main() {     // normal boost::asio setup     std::string const host = "echo.websocket.org";     boost::asio::io_service ios;     boost::asio::ip::tcp::resolver r(ios);     boost::asio::ip::tcp::socket sock(ios);     boost::asio::connect(sock,         r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));      // websocket connect , send message using beast     beast::websocket::stream<boost::asio::ip::tcp::socket&> ws(sock);     ws.handshake(host, "/");     ws.write(boost::asio::buffer("hello, world!"));      // receive websocket message, print , close using beast     beast::streambuf sb;     beast::websocket::opcode op;     ws.read(op, sb);     ws.close(beast::websocket::close_code::normal);     std::cout << to_string(sb.data()) << "\n"; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -