2 Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 [section:example Examples]
10 These usage examples are intended to quickly impress upon readers the
11 flavor of the library. They are complete programs which may be built
12 and run. Source code and build scripts for these programs may be found
13 in the examples directory.
17 Use HTTP to request the root page from a website and print the response:
20 #include <beast/http.hpp>
21 #include <boost/asio.hpp>
22 #include <boost/lexical_cast.hpp>
28 // Normal boost::asio setup
29 std::string const host = "boost.org";
30 boost::asio::io_service ios;
31 boost::asio::ip::tcp::resolver r{ios};
32 boost::asio::ip::tcp::socket sock{ios};
33 boost::asio::connect(sock,
34 r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}));
36 // Send HTTP request using beast
37 beast::http::request<beast::http::string_body> req;
41 req.fields.replace("Host", host + ":" +
42 boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
43 req.fields.replace("User-Agent", "Beast");
44 beast::http::prepare(req);
45 beast::http::write(sock, req);
47 // Receive and print HTTP response using beast
49 beast::http::response<beast::http::streambuf_body> resp;
50 beast::http::read(sock, sb, resp);
56 Establish a WebSocket connection, send a message and receive the reply:
58 #include <beast/core/to_string.hpp>
59 #include <beast/websocket.hpp>
60 #include <boost/asio.hpp>
66 // Normal boost::asio setup
67 std::string const host = "echo.websocket.org";
68 boost::asio::io_service ios;
69 boost::asio::ip::tcp::resolver r{ios};
70 boost::asio::ip::tcp::socket sock{ios};
71 boost::asio::connect(sock,
72 r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));
74 // WebSocket connect and send message using beast
75 beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
76 ws.handshake(host, "/");
77 ws.write(boost::asio::buffer(std::string("Hello, world!")));
79 // Receive WebSocket message, print and close using beast
81 beast::websocket::opcode op;
83 ws.close(beast::websocket::close_code::normal);
84 std::cout << beast::to_string(sb.data()) << "\n";
88 [heading WebSocket Echo Server]
90 This example demonstrates both synchronous and asynchronous
91 WebSocket server implementations.
93 * [@examples/websocket_async_echo_server.hpp]
94 * [@examples/websocket_sync_echo_server.hpp]
95 * [@examples/websocket_echo.cpp]
97 [heading Secure WebSocket]
99 Establish a WebSocket connection over an encrypted TLS connection,
100 send a message and receive the reply. Requires OpenSSL to build.
102 * [@examples/websocket_ssl_example.cpp]
106 This example demonstrates sending and receiving HTTP messages
107 over a TLS connection. Requires OpenSSL to build.
109 * [@examples/http_ssl_example.cpp]
113 This example retrieves the page at each of the most popular domains
114 as measured by Alexa.
116 * [@examples/http_crawl.cpp]
118 [heading HTTP Server]
120 This example demonstrates both synchronous and asynchronous server
121 implementations. It also provides an example of implementing a [*Body]
122 type, in `file_body`.
124 * [@examples/file_body.hpp]
125 * [@examples/http_async_server.hpp]
126 * [@examples/http_sync_server.hpp]
127 * [@examples/http_server.cpp]
131 These are stand-alone listings of the HTTP and WebSocket examples.
133 * [@examples/http_example.cpp]
134 * [@examples/websocket_example.cpp]