Websocket ++ simple example, websocket example

Source: Internet
Author: User

Websocket ++ simple example, websocket example

Preface

Html5 supports the use of websocket Protocol to maintain a persistent connection with the server, so that both parties can transmit data to each other, and the server can also actively send information to the client, which is difficult to do before using HTTP. The following describes the simple use of websocket ++ implemented using C ++. For more information about websocket ++, click here.

Websocket ++ requires boost support, so the project must contain the boost header file and library. For how to use boost in VS, refer to this article.

C ++ code

1 # include "stdafx. h "2 # include <iostream> 3 4 using namespace std; 5 6 # include <websocketpp/config/asio_no_tls.hpp> 7 # include <websocketpp/server. hpp> 8 9 typedef websocketpp: server <websocketpp: config: asio> WebsocketServer; 10 typedef WebsocketServer: message_ptr; 11 12 using websocketpp: lib: placeholders :: _ 1; 13 using websocketpp: lib: placeholders: _ 2; 14 using websocketpp: lib: bind; 15 16 void OnOpen (WebsocketServer * server, websocketpp :: connection_hdl hdl) 17 {18 cout <"have client connected" <endl; 19} 20 21 void OnClose (WebsocketServer * server, websocketpp: connection_hdl) 22 {23 cout <"have client disconnected" <endl; 24} 25 26 void OnMessage (WebsocketServer * server, websocketpp: connection_hdl, message_ptr msg) 27 {28 string strMsg = msg-> get_payload (); 29 cout <strMsg <endl; 30 31 string strRespon = "receive:"; 32 strRespon. append (strMsg); 33 34 server-> send (hdl, strRespon, websocketpp: frame: opcode: text); 35} 36 37 int main () 38 {39 WebsocketServer server; 40 41 // Set logging settings42 server. set_access_channels (websocketpp: log: alevel: all); 43 server. clear_access_channels (websocketpp: log: alevel: frame_payload); 44 45 // Initialize ASIO46 server. init_asio (); 47 48 // Register our open handler49 server. set_open_handler (bind (& OnOpen, & server,: _ 1); 50 51 // Register our close handler52 server. set_close_handler (bind (& OnClose, & server, _ 1); 53 54 // Register our message handler55 server. set_message_handler (bind (& OnMessage, & server, _ 1, _ 2); 56 57 // Listen on port 215258 server. listen (8090); 59 60 // Start the server accept loop61 server. start_accept (); 62 63 // Start the ASIO io_service run loop64 server. run (); 65 66 return 0; 67}View Code

Html code

1 <! DOCTYPE html> 2 Running result

 

 

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.