forked from boostorg/beast
Refactor WebSocket, HTTP examples:
fix #297, fix #488 * Errors are checked and reported * More comments explaining what is going on * The connection is gracefully closed WebSocket: * Messages are drained before closing
This commit is contained in:
@@ -10,29 +10,89 @@
|
||||
#include <beast/core.hpp>
|
||||
#include <beast/websocket.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Normal boost::asio setup
|
||||
std::string const host = "echo.websocket.org";
|
||||
// A helper for reporting errors
|
||||
auto const fail =
|
||||
[](std::string what, beast::error_code ec)
|
||||
{
|
||||
std::cerr << what << ": " << ec.message() << std::endl;
|
||||
std::cerr.flush();
|
||||
return EXIT_FAILURE;
|
||||
};
|
||||
|
||||
boost::system::error_code ec;
|
||||
|
||||
// Set up an asio socket
|
||||
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
|
||||
// Look up the domain name
|
||||
std::string const host = "echo.websocket.org";
|
||||
auto const lookup = r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}, ec);
|
||||
if(ec)
|
||||
return fail("resolve", ec);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
boost::asio::connect(sock, lookup, ec);
|
||||
if(ec)
|
||||
return fail("connect", ec);
|
||||
|
||||
// Wrap the now-connected socket in a websocket stream
|
||||
beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
|
||||
ws.handshake(host, "/");
|
||||
ws.write(boost::asio::buffer(std::string("Hello, world!")));
|
||||
|
||||
// Receive WebSocket message, print and close using beast
|
||||
// Perform the websocket handhskae
|
||||
ws.handshake(host, "/", ec);
|
||||
if(ec)
|
||||
return fail("handshake", ec);
|
||||
|
||||
// Send a message
|
||||
ws.write(boost::asio::buffer(std::string("Hello, world!")), ec);
|
||||
if(ec)
|
||||
return fail("write", ec);
|
||||
|
||||
// This buffer will hold the incoming message
|
||||
beast::multi_buffer b;
|
||||
ws.read(b);
|
||||
ws.close(beast::websocket::close_code::normal);
|
||||
std::cout << beast::buffers(b.data()) << "\n";
|
||||
|
||||
// Read the message into our buffer
|
||||
ws.read(b, ec);
|
||||
if(ec)
|
||||
return fail("read", ec);
|
||||
|
||||
// Send a "close" frame to the other end, this is a websocket thing
|
||||
ws.close(beast::websocket::close_code::normal, ec);
|
||||
if(ec)
|
||||
return fail("close", ec);
|
||||
|
||||
// The buffers() function helps print a ConstBufferSequence
|
||||
std::cout << beast::buffers(b.data()) << std::endl;
|
||||
|
||||
// WebSocket says that to close a connection you have
|
||||
// to keep reading messages until you receive a close frame.
|
||||
// Beast delivers the close frame as an error from read.
|
||||
//
|
||||
beast::drain_buffer drain; // Throws everything away efficiently
|
||||
for(;;)
|
||||
{
|
||||
// Keep reading messages...
|
||||
ws.read(drain, ec);
|
||||
|
||||
// ...until we get the special error code
|
||||
if(ec == beast::websocket::error::closed)
|
||||
break;
|
||||
|
||||
// Some other error occurred, report it and exit.
|
||||
if(ec)
|
||||
return fail("close", ec);
|
||||
}
|
||||
|
||||
// If we get here the connection was cleanly closed
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
Reference in New Issue
Block a user