]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/example/websocket/server/chat-multi/websocket_session.cpp
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 #include "websocket_session.hpp"
16 boost::shared_ptr
<shared_state
> const& state
)
17 : ws_(std::move(socket
))
25 // Remove this session from the list of active sessions
31 fail(beast::error_code ec
, char const* what
)
34 if( ec
== net::error::operation_aborted
||
35 ec
== websocket::error::closed
)
38 std::cerr
<< what
<< ": " << ec
.message() << "\n";
43 on_accept(beast::error_code ec
)
45 // Handle the error, if any
47 return fail(ec
, "accept");
49 // Add this session to the list of active sessions
55 beast::bind_front_handler(
56 &websocket_session::on_read
,
62 on_read(beast::error_code ec
, std::size_t)
64 // Handle the error, if any
66 return fail(ec
, "read");
68 // Send to all connections
69 state_
->send(beast::buffers_to_string(buffer_
.data()));
72 buffer_
.consume(buffer_
.size());
74 // Read another message
77 beast::bind_front_handler(
78 &websocket_session::on_read
,
84 send(boost::shared_ptr
<std::string
const> const& ss
)
86 // Post our work to the strand, this ensures
87 // that the members of `this` will not be
88 // accessed concurrently.
92 beast::bind_front_handler(
93 &websocket_session::on_send
,
100 on_send(boost::shared_ptr
<std::string
const> const& ss
)
102 // Always add to queue
103 queue_
.push_back(ss
);
105 // Are we already writing?
106 if(queue_
.size() > 1)
109 // We are not currently writing, so send this immediately
111 net::buffer(*queue_
.front()),
112 beast::bind_front_handler(
113 &websocket_session::on_write
,
114 shared_from_this()));
119 on_write(beast::error_code ec
, std::size_t)
121 // Handle the error, if any
123 return fail(ec
, "write");
125 // Remove the string from the queue
126 queue_
.erase(queue_
.begin());
128 // Send the next message if any
131 net::buffer(*queue_
.front()),
132 beast::bind_front_handler(
133 &websocket_session::on_write
,
134 shared_from_this()));