15 : _id(CppCommon::UUID::Generate()),
22 _messages_received(0),
26 assert((
service !=
nullptr) &&
"ASIO service is invalid!");
28 throw CppCommon::ArgumentException(
"ASIO service is invalid!");
30 assert((
context !=
nullptr) &&
"SSL context is invalid!");
32 throw CppCommon::ArgumentException(
"SSL context is invalid!");
37 void WebSocketSSLClient::InitAsio()
39 assert(!_initialized &&
"Asio is already initialed!");
44 websocketpp::lib::error_code ec;
45 _core.init_asio(_service->service().get(), ec);
57 assert(_initialized &&
"Asio is not initialed!");
65 auto self(this->shared_from_this());
66 _service->service()->post([
this,
self]()
68 websocketpp::lib::error_code ec;
71 _core.set_access_channels(websocketpp::log::alevel::none);
72 _core.set_error_channels(websocketpp::log::elevel::none);
75 _core.set_open_handler([
this](websocketpp::connection_hdl connection) { Connected(connection); });
76 _core.set_close_handler([
this](websocketpp::connection_hdl connection) { Disconnected(connection); });
77 _core.set_tls_init_handler([
this](websocketpp::connection_hdl connection) {
return _context; });
80 WebSocketSSLClientCore::connection_ptr connection_ptr = _core.get_connection(_uri, ec);
89 connection_ptr->set_message_handler([
this](websocketpp::connection_hdl connection,
WebSocketSSLMessage message)
91 size_t size = message->get_raw_payload().size();
95 _bytes_received += size;
100 connection_ptr->set_fail_handler([
this](websocketpp::connection_hdl connection)
102 WebSocketSSLServerCore::connection_ptr con = _core.get_con_from_hdl(connection);
103 websocketpp::lib::error_code ec = con->get_ec();
105 Disconnected(connection);
110 _core.connect(connection_ptr);
116 void WebSocketSSLClient::Connected(websocketpp::connection_hdl connection)
120 _messages_received = 0;
125 _connection = connection;
137 auto self(this->shared_from_this());
138 auto disconnect = [
this,
self, code, reason]()
141 websocketpp::lib::error_code ec;
142 _core.close(_connection, code, reason, ec);
149 _service->Dispatch(disconnect);
151 _service->Post(disconnect);
156 void WebSocketSSLClient::Disconnected(websocketpp::connection_hdl connection)
172 CppCommon::Thread::Yield();
179 assert((buffer !=
nullptr) &&
"Pointer to the buffer should not be equal to 'nullptr'!");
180 assert((size > 0) &&
"Buffer size should be greater than zero!");
181 if ((buffer ==
nullptr) || (size == 0))
187 websocketpp::lib::error_code ec;
188 _core.send(_connection, buffer, size, opcode, ec);
207 websocketpp::lib::error_code ec;
208 _core.send(_connection, text, opcode, ec);
215 size_t size = text.size();
229 websocketpp::lib::error_code ec;
230 _core.send(_connection, message, ec);
237 size_t size = message->get_raw_payload().size();
246 void WebSocketSSLClient::SendError(std::error_code ec)
248 onError(ec.value(), ec.category().name(), ec.message());
virtual void onConnected()
Handle client connected notification.
WebSocketSSLClient(std::shared_ptr< Service > service, std::shared_ptr< asio::ssl::context > context, const std::string &uri)
Initialize WebSocket client with a given Asio service, SSL context and server URI address...
std::shared_ptr< asio::ssl::context > & context() noexcept
Get the client SSL context.
bool Disconnect(websocketpp::close::status::value code=websocketpp::close::status::normal, const std::string &reason="")
Disconnect the client.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
C++ Server project definitions.
WebSocketSSLConnection::message_ptr WebSocketSSLMessage
WebSocket SSL message.
virtual void onReceived(const WebSocketSSLMessage &message)
Handle message received notification.
size_t Send(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Send data to the server.
bool Connect()
Connect the client.
WebSocket SSL client definition.
bool Reconnect()
Reconnect the client.
virtual void onDisconnected()
Handle client disconnected notification.
bool IsConnected() const noexcept
Is the client connected?
std::shared_ptr< Service > & service() noexcept
Get the Asio service.