| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | [/ | 
					
						
							| 
									
										
										
										
											2017-02-06 20:07:03 -05:00
										 |  |  |     Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Distributed under the Boost Software License, Version 1.0. (See accompanying | 
					
						
							|  |  |  |     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [section:example Examples] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | These usage examples are intended to quickly impress upon readers the | 
					
						
							|  |  |  | flavor of the library. They are complete programs which may be built | 
					
						
							|  |  |  | and run. Source code and build scripts for these programs may be found | 
					
						
							|  |  |  | in the examples directory. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [heading HTTP GET] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Use HTTP to request the root page from a website and print the response: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | #include <beast/http.hpp> | 
					
						
							|  |  |  | #include <boost/asio.hpp> | 
					
						
							| 
									
										
										
										
											2017-01-08 07:00:04 -05:00
										 |  |  | #include <boost/lexical_cast.hpp> | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | #include <iostream> | 
					
						
							|  |  |  | #include <string> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Normal boost::asio setup | 
					
						
							|  |  |  |     std::string const host = "boost.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, "http"})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Send HTTP request using beast | 
					
						
							| 
									
										
										
										
											2016-11-20 07:32:41 -05:00
										 |  |  |     beast::http::request<beast::http::string_body> req; | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  |     req.method = "GET"; | 
					
						
							|  |  |  |     req.url = "/"; | 
					
						
							|  |  |  |     req.version = 11; | 
					
						
							| 
									
										
										
										
											2017-01-08 07:00:04 -05:00
										 |  |  |     req.fields.replace("Host", host + ":" + | 
					
						
							|  |  |  |         boost::lexical_cast<std::string>(sock.remote_endpoint().port())); | 
					
						
							|  |  |  |     req.fields.replace("User-Agent", "Beast"); | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  |     beast::http::prepare(req); | 
					
						
							|  |  |  |     beast::http::write(sock, req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Receive and print HTTP response using beast | 
					
						
							|  |  |  |     beast::streambuf sb; | 
					
						
							| 
									
										
										
										
											2017-01-08 07:00:04 -05:00
										 |  |  |     beast::http::response<beast::http::streambuf_body> resp; | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  |     beast::http::read(sock, sb, resp); | 
					
						
							|  |  |  |     std::cout << resp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | [heading WebSocket] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Establish a WebSocket connection, send a message and receive the reply: | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | #include <beast/core/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 and send message using beast | 
					
						
							|  |  |  |     beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock}; | 
					
						
							|  |  |  |     ws.handshake(host, "/"); | 
					
						
							| 
									
										
										
										
											2017-01-08 07:00:04 -05:00
										 |  |  |     ws.write(boost::asio::buffer(std::string("Hello, world!"))); | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Receive WebSocket message, print and close using beast | 
					
						
							|  |  |  |     beast::streambuf sb; | 
					
						
							|  |  |  |     beast::websocket::opcode op; | 
					
						
							|  |  |  |     ws.read(op, sb); | 
					
						
							|  |  |  |     ws.close(beast::websocket::close_code::normal); | 
					
						
							| 
									
										
										
										
											2017-01-08 07:00:04 -05:00
										 |  |  |     std::cout << beast::to_string(sb.data()) << "\n"; | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 09:53:10 -05:00
										 |  |  | [heading WebSocket Echo Server] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This example demonstrates both synchronous and asynchronous | 
					
						
							|  |  |  | WebSocket server implementations. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/websocket_async_echo_server.hpp] | 
					
						
							| 
									
										
										
										
											2017-02-22 13:19:07 +01:00
										 |  |  | * [@examples/websocket_sync_echo_server.hpp] | 
					
						
							| 
									
										
										
										
											2017-01-30 09:53:10 -05:00
										 |  |  | * [@examples/websocket_echo.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 16:42:50 -04:00
										 |  |  | [heading Secure WebSocket] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Establish a WebSocket connection over an encrypted TLS connection, | 
					
						
							|  |  |  | send a message and receive the reply. Requires OpenSSL to build. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/websocket_ssl_example.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 16:33:42 -04:00
										 |  |  | [heading HTTPS GET] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This example demonstrates sending and receiving HTTP messages | 
					
						
							|  |  |  | over a TLS connection. Requires OpenSSL to build. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/http_ssl_example.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 11:19:51 -04:00
										 |  |  | [heading HTTP Crawl] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This example retrieves the page at each of the most popular domains | 
					
						
							|  |  |  | as measured by Alexa. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/http_crawl.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [heading HTTP Server] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This example demonstrates both synchronous and asynchronous server | 
					
						
							|  |  |  | implementations. It also provides an example of implementing a [*Body] | 
					
						
							|  |  |  | type, in `file_body`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/file_body.hpp] | 
					
						
							|  |  |  | * [@examples/http_async_server.hpp] | 
					
						
							|  |  |  | * [@examples/http_sync_server.hpp] | 
					
						
							|  |  |  | * [@examples/http_server.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [heading Listings] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | These are stand-alone listings of the HTTP and WebSocket examples. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [@examples/http_example.cpp] | 
					
						
							|  |  |  | * [@examples/websocket_example.cpp] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [endsect] |