]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
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) | |
6 | // | |
7 | ||
8 | #include <beast/core/to_string.hpp> | |
9 | #include <beast/websocket.hpp> | |
10 | #include <boost/asio.hpp> | |
11 | #include <iostream> | |
12 | #include <string> | |
13 | ||
14 | int main() | |
15 | { | |
16 | // Normal boost::asio setup | |
17 | std::string const host = "echo.websocket.org"; | |
18 | boost::asio::io_service ios; | |
19 | boost::asio::ip::tcp::resolver r{ios}; | |
20 | boost::asio::ip::tcp::socket sock{ios}; | |
21 | boost::asio::connect(sock, | |
22 | r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"})); | |
23 | ||
24 | // WebSocket connect and send message using beast | |
25 | beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock}; | |
26 | ws.handshake(host, "/"); | |
27 | ws.write(boost::asio::buffer(std::string("Hello, world!"))); | |
28 | ||
29 | // Receive WebSocket message, print and close using beast | |
30 | beast::streambuf sb; | |
31 | beast::websocket::opcode op; | |
32 | ws.read(op, sb); | |
33 | ws.close(beast::websocket::close_code::normal); | |
34 | std::cout << beast::to_string(sb.data()) << "\n"; | |
35 | } |