Files
beast/example/websocket-client/websocket_client.cpp

102 lines
2.7 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07: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)
//
//[example_websocket_client
2017-06-04 17:25:55 -07:00
#include <beast/core.hpp>
2017-07-20 08:01:46 -07:00
#include <beast/websocket.hpp>
#include <boost/asio.hpp>
#include <cstdlib>
2017-07-20 08:01:46 -07:00
#include <iostream>
#include <string>
2017-06-19 14:41:28 -07:00
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace websocket = beast::websocket; // from <beast/websocket.hpp>
2017-07-20 08:01:46 -07:00
int main()
{
// 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
2017-07-20 08:01:46 -07:00
boost::asio::io_service ios;
2017-06-19 14:41:28 -07:00
tcp::resolver r{ios};
tcp::socket sock{ios};
2017-07-20 08:01:46 -07:00
// Look up the domain name
std::string const host = "echo.websocket.org";
2017-06-19 07:28:20 -07:00
auto const lookup = r.resolve({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
2017-06-19 14:41:28 -07:00
websocket::stream<tcp::socket&> ws{sock};
2017-07-20 08:01:46 -07:00
// Perform the websocket handshake
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;
// 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
2017-06-19 14:41:28 -07:00
ws.close(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
2017-06-19 14:41:28 -07:00
if(ec == 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;
2017-07-20 08:01:46 -07:00
}
2017-06-04 17:25:55 -07:00
//]