websocket++ simple use and instance analysis _c language

Source: Internet
Author: User

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

<!
 DOCTYPE html>  

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.