20 using connection_handler = websocketpp::connection_hdl;
21 Nano::Signal<void()> on_open;
22 Nano::Signal<void()> on_close;
23 Nano::Signal<void()> on_fail;
33 m_client = std::make_shared<client_t>();
35 std::weak_ptr<client_t> weak_client = m_client;
36 m_client->clear_access_channels(websocketpp::log::alevel::all);
37 m_client->clear_error_channels(websocketpp::log::elevel::all);
39 m_client->set_open_handler([
this, weak_client](connection_handler hdl) {
40 if(!weak_client.lock())
42 scoped_lock guard(m_lock);
48 m_client->set_close_handler([
this, weak_client](connection_handler hdl) {
49 if(!weak_client.lock())
52 scoped_lock guard(m_lock);
58 m_client->set_fail_handler([
this, weak_client](connection_handler hdl) {
59 if(!weak_client.lock())
62 scoped_lock guard(m_lock);
72 template <
typename MessageHandler>
77 m_client->init_asio();
79 std::weak_ptr<client_t> weak_client = m_client;
80 m_client->set_message_handler(
81 [handler = std::move(onMessage),
82 weak_client](connection_handler hdl, client_t::message_ptr msg) {
83 if(!weak_client.lock())
85 handler(hdl, msg->get_opcode(), msg->get_raw_payload());
90 template <
typename MessageHandler>
95 m_client->init_asio(&ctx);
98 std::weak_ptr<client_t> weak_client = m_client;
99 m_client->set_message_handler(
100 [handler = std::move(onMessage),
101 weak_client](connection_handler hdl, client_t::message_ptr msg) {
102 if(!weak_client.lock())
104 handler(hdl, msg->get_opcode(), msg->get_raw_payload());
115 bool connected()
const {
return m_open; }
127 scoped_lock guard(m_lock);
128 m_client->close(m_hdl, websocketpp::close::status::normal,
"");
133 auto& client() {
return m_client; }
134 auto& handle() {
return m_hdl; }
135 bool after_connect() {
return m_connected; }
137 void connect(
const std::string& uri)
139 websocketpp::lib::error_code ec;
145 m_client->init_asio(m_ctx);
147 m_client->init_asio();
150 auto con = m_client->get_connection(uri, ec);
153 m_client->get_alog().write(
154 websocketpp::log::alevel::app,
"Get Connection Error: " + ec.message());
158 m_hdl = con->get_handle();
159 m_client->connect(con);
163 void finish_connection()
170 void connect_and_run(
const std::string& uri)
179 void send_message(
const std::string& request)
181 if(!m_open || !m_client)
184 websocketpp::lib::error_code ec;
186 m_client->send(m_hdl, request, websocketpp::frame::opcode::text, ec);
190 m_client->get_alog().write(
191 websocketpp::log::alevel::app,
"Send Error: " + ec.message());
195 void send_message(
const rapidjson::StringBuffer& request)
197 if(!m_open || !m_client)
200 websocketpp::lib::error_code ec;
203 m_hdl, request.GetString(), request.GetSize(), websocketpp::frame::opcode::text,
208 m_client->get_alog().write(
209 websocketpp::log::alevel::app,
"Send Error: " + ec.message());
213 void send_binary_message(std::string_view request)
215 if(!m_open || !m_client)
218 websocketpp::lib::error_code ec;
221 m_hdl, request.data(), request.size(), websocketpp::frame::opcode::binary, ec);
225 m_client->get_alog().write(
226 websocketpp::log::alevel::app,
"Send Error: " + ec.message());
231 using client_t = websocketpp::client<websocketpp::config::asio_client>;
232 using scoped_lock = websocketpp::lib::lock_guard<websocketpp::lib::mutex>;
234 boost::asio::io_context* m_ctx{};
235 std::shared_ptr<client_t> m_client;
236 connection_handler m_hdl;
237 websocketpp::lib::mutex m_lock;
238 std::atomic_bool m_open{
false};
239 std::atomic_bool m_connected{
false};