Objective
HTML5 supports the use of the WebSocket protocol to maintain a long connection with the server, to facilitate the transfer of data between the two parties, and the server can actively send information to the client, and before this use of HTTP is difficult to do. The following describes the simple use of websocket++ implemented using C + +. websocket++ more details on this point.
websocket++ needs boost support, so the header files and libraries that include boost are needed in the project. How boost is used in VS is referenced in this article.
C + + code
#include "stdafx.h" #include <iostream> using namespace std; #include <websocketpp/config/asio_no_tls.hpp> #include <websocketpp/server.hpp> typedef WEBSOCKETPP::
Server<websocketpp::config::asio> Websocketserver;
typedef WEBSOCKETSERVER::MESSAGE_PTR MESSAGE_PTR;
Using websocketpp::lib::p laceholders::_1;
Using websocketpp::lib::p laceholders::_2;
Using Websocketpp::lib::bind; void OnOpen (Websocketserver *server, Websocketpp::connection_hdl HDL) {cout << "have client connected" << E
Ndl } void OnClose (Websocketserver *server, Websocketpp::connection_hdl HDL) {cout << "have client Disconnected"
;< Endl; } void OnMessage (Websocketserver *server, websocketpp::connection_hdl HDL, Message_ptr msg) {string strMsg = Msg->g
Et_payload ();
cout << strMsg << Endl;
String strrespon = "Receive:";
Strrespon.append (STRMSG);
Server->send (HDL, Strrespon, Websocketpp::frame::opcode::text); int main () {WebsockEtserver server;
Set Logging Settings server.set_access_channels (websocketpp::log::alevel::all);
Server.clear_access_channels (Websocketpp::log::alevel::frame_payload);
Initialize ASIO Server.init_asio ();
Register our Open handler Server.set_open_handler (Bind (&onopen, &server,:: _1));
Register our close handler Server.set_close_handler (Bind (&onclose, &server, _1));
Register our message Handler Server.set_message_handler (Bind (&onmessage, &server, _1, _2));
Listen on port 2152 server.listen (8090);
Start the server Accept Loop server.start_accept ();
Start the ASIO io_service run Loop server.run ();
return 0; }
html code
Run results
The above websocket++ simple use and case analysis is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.