2 // Copyright (c) 2016-2019 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)
7 // Official repository: https://github.com/vinniefalco/CppCon2018
10 #ifndef BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_WEBSOCKET_SESSION_HPP
11 #define BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_WEBSOCKET_SESSION_HPP
15 #include "shared_state.hpp"
22 // Forward declaration
25 /** Represents an active WebSocket connection to the server
27 class websocket_session : public boost::enable_shared_from_this<websocket_session>
29 beast::flat_buffer buffer_;
30 websocket::stream<beast::tcp_stream> ws_;
31 boost::shared_ptr<shared_state> state_;
32 std::vector<boost::shared_ptr<std::string const>> queue_;
34 void fail(beast::error_code ec, char const* what);
35 void on_accept(beast::error_code ec);
36 void on_read(beast::error_code ec, std::size_t bytes_transferred);
37 void on_write(beast::error_code ec, std::size_t bytes_transferred);
42 boost::shared_ptr<shared_state> const& state);
46 template<class Body, class Allocator>
48 run(http::request<Body, http::basic_fields<Allocator>> req);
52 send(boost::shared_ptr<std::string const> const& ss);
56 on_send(boost::shared_ptr<std::string const> const& ss);
59 template<class Body, class Allocator>
62 run(http::request<Body, http::basic_fields<Allocator>> req)
64 // Set suggested timeout settings for the websocket
66 websocket::stream_base::timeout::suggested(
67 beast::role_type::server));
69 // Set a decorator to change the Server of the handshake
70 ws_.set_option(websocket::stream_base::decorator(
71 [](websocket::response_type& res)
73 res.set(http::field::server,
74 std::string(BOOST_BEAST_VERSION_STRING) +
75 " websocket-chat-multi");
78 // Accept the websocket handshake
81 beast::bind_front_handler(
82 &websocket_session::on_accept,