From cb049223fcf8edf3d65a13369da84047fa2f409a Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Fri, 23 Jun 2023 15:39:50 +0800 Subject: [PATCH] no message --- test/src/websocket_bench/websocket_bench.cpp | 9 +- test/src/websocket_bench/websocket_client.cpp | 274 ++++++++++++++---- test/src/websocket_bench/websocket_client.h | 1 + 3 files changed, 219 insertions(+), 65 deletions(-) diff --git a/test/src/websocket_bench/websocket_bench.cpp b/test/src/websocket_bench/websocket_bench.cpp index 201a15c..85ff246 100644 --- a/test/src/websocket_bench/websocket_bench.cpp +++ b/test/src/websocket_bench/websocket_bench.cpp @@ -4,12 +4,13 @@ int main(int argc,char **argv){ std::cout<<"dfasdfasd"<m_status = WebsocketClient::STOP; - + } -WebsocketClient::WebsocketClient(std::string url, bool tls) -{ +WebsocketClient::WebsocketClient(std::string url, bool tls) { m_tls = tls; m_auto_reconn = false; m_url = url; @@ -135,66 +134,65 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) }); // Initialize ASIO m_client_tls.init_asio(); - m_thread = new std::thread([this]() + m_thread = new std::thread([this]() { + while (this->m_status != STOP) { - while (this->m_status != STOP) - { - m_client_tls.set_message_handler(bind(&on_message, this, ::_1, ::_2)); - websocketpp::lib::error_code ec; - std::cout << "1" << std::endl; - m_conn_tls = m_client_tls.get_connection(this->m_url, ec); - if (ec) { - std::cout << "could not create connection because: " << ec.message() << std::endl; - this->m_status = Status::FAIL; - break; - } - m_conn_tls->set_open_handler(websocketpp::lib::bind( - &on_open, - this, - websocketpp::lib::placeholders::_1 - )); - m_conn_tls->set_fail_handler(websocketpp::lib::bind( - &on_fail, - this, - websocketpp::lib::placeholders::_1 - )); - m_conn_tls->set_close_handler(websocketpp::lib::bind( - &on_close, - this, - websocketpp::lib::placeholders::_1 - )); - - std::cout << "2" << std::endl; - // Note that connect here only requests a connection. No network messages are - // exchanged until the event loop starts running in the next line. - TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls); - if (ptr->get_state() != websocketpp::session::state::open) - std::cout << ptr->get_state() << " websocketpp::session::state " << std::endl; - // Start the ASIO io_service run loop - // this will cause a single connection to be made to the server. c.run() - // will exit when this connection is closed. - std::cout << "3 " << ptr << " " << m_conn_tls << std::endl; - this->m_status = WebsocketClient::CONNECTING; - while ((this->m_status != WebsocketClient::FAIL) && - (this->m_status != WebsocketClient::CLOSED) - && (this->m_status != WebsocketClient::STOP) - ) { - try { - int count_of_handler = this->m_client_tls.run(); - std::cout << "4 " << std::endl; - - // run应该只执行一次就会退出 - } - catch (std::exception e) { - std::cout << "run exception" << e.what(); - } - } - Sleep(1000); + m_client_tls.set_message_handler(bind(&on_message, this, ::_1, ::_2)); + websocketpp::lib::error_code ec; + std::cout << "1" << std::endl; + m_conn_tls = m_client_tls.get_connection(this->m_url, ec); + if (ec) { + std::cout << "could not create connection because: " << ec.message() << std::endl; + this->m_status = Status::FAIL; + break; } - std::cout << "close"; - if(m_on_disconnected) - this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); - }); + m_conn_tls->set_open_handler(websocketpp::lib::bind( + &on_open, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn_tls->set_fail_handler(websocketpp::lib::bind( + &on_fail, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn_tls->set_close_handler(websocketpp::lib::bind( + &on_close, + this, + websocketpp::lib::placeholders::_1 + )); + + std::cout << "2" << std::endl; + // Note that connect here only requests a connection. No network messages are + // exchanged until the event loop starts running in the next line. + TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls); + if (ptr->get_state() != websocketpp::session::state::open) + std::cout << ptr->get_state() << " websocketpp::session::state " << std::endl; + // Start the ASIO io_service run loop + // this will cause a single connection to be made to the server. c.run() + // will exit when this connection is closed. + std::cout << "3 " << ptr << " " << m_conn_tls << std::endl; + this->m_status = WebsocketClient::CONNECTING; + while ((this->m_status != WebsocketClient::FAIL) && + (this->m_status != WebsocketClient::CLOSED) + && (this->m_status != WebsocketClient::STOP) + ) { + try { + int count_of_handler = this->m_client_tls.run(); + std::cout << "4 " << std::endl; + + // run应该只执行一次就会退出 + } + catch (std::exception e) { + std::cout << "run exception" << e.what(); + } + } + Sleep(1000); + } + std::cout << "close"; + if(m_on_disconnected) + this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); + }); } else { // Set logging to be pretty verbose (everything except message payloads) @@ -273,6 +271,160 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) } + +int WebsocketClient::Connect(std::string url){ + m_url = url; + this->m_status = WebsocketClient::CONNECTING; + + if (m_tls) { + // Set logging to be pretty verbose (everything except message payloads) + m_client_tls.set_access_channels(websocketpp::log::alevel::all); + m_client_tls.clear_access_channels(websocketpp::log::alevel::frame_payload); + m_client_tls.set_tls_init_handler([this](websocketpp::connection_hdl) { + return websocketpp::lib::make_shared(asio::ssl::context::tlsv1); + }); + // Initialize ASIO + m_client_tls.init_asio(); + m_thread = new std::thread([this]() { + while (this->m_status != STOP) + { + m_client_tls.set_message_handler(bind(&on_message, this, ::_1, ::_2)); + websocketpp::lib::error_code ec; + std::cout << "1" << std::endl; + m_conn_tls = m_client_tls.get_connection(this->m_url, ec); + if (ec) { + std::cout << "could not create connection because: " << ec.message() << std::endl; + this->m_status = Status::FAIL; + break; + } + m_conn_tls->set_open_handler(websocketpp::lib::bind( + &on_open, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn_tls->set_fail_handler(websocketpp::lib::bind( + &on_fail, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn_tls->set_close_handler(websocketpp::lib::bind( + &on_close, + this, + websocketpp::lib::placeholders::_1 + )); + + std::cout << "2" << std::endl; + // Note that connect here only requests a connection. No network messages are + // exchanged until the event loop starts running in the next line. + TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls); + if (ptr->get_state() != websocketpp::session::state::open) + std::cout << ptr->get_state() << " websocketpp::session::state " << std::endl; + // Start the ASIO io_service run loop + // this will cause a single connection to be made to the server. c.run() + // will exit when this connection is closed. + std::cout << "3 " << ptr << " " << m_conn_tls << std::endl; + this->m_status = WebsocketClient::CONNECTING; + while ((this->m_status != WebsocketClient::FAIL) && + (this->m_status != WebsocketClient::CLOSED) + && (this->m_status != WebsocketClient::STOP) + ) { + try { + int count_of_handler = this->m_client_tls.run(); + std::cout << "4 " << std::endl; + + // run应该只执行一次就会退出 + } + catch (std::exception e) { + std::cout << "run exception" << e.what(); + } + } + Sleep(1000); + } + std::cout << "close"; + if(m_on_disconnected) + this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); + }); + } + else { + + // Set logging to be pretty verbose (everything except message payloads) + m_client.set_access_channels(websocketpp::log::alevel::all); + m_client.clear_access_channels(websocketpp::log::alevel::frame_payload); + + // Initialize ASIO + this->m_client.init_asio(); + m_thread = new std::thread([this]() + { + while (this->m_status != STOP) + { + this->m_client.set_message_handler(bind(&on_message, this, ::_1, ::_2)); + websocketpp::lib::error_code ec; + std::cout << "1" << std::endl; + m_conn = m_client.get_connection(this->m_url, ec); + if (m_conn != nullptr) { + m_conn->set_open_handler(websocketpp::lib::bind( + &on_open, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn->set_fail_handler(websocketpp::lib::bind( + &on_fail, + this, + websocketpp::lib::placeholders::_1 + )); + m_conn->set_close_handler(websocketpp::lib::bind( + &on_close, + this, + websocketpp::lib::placeholders::_1 + )); + } + if (ec) { + std::cout << "could not create connection because: " << ec.message() << std::endl; + this->m_status = Status::FAIL; + if (m_on_disconnected) + m_on_disconnected(this, + WebsocketClient::CloseReason::LOCAL_CLOSED); + break; + } + + // Note that connect here only requests a connection. No network messages are + // exchanged until the event loop starts running in the next line. + Client::connection_ptr ptr = this->m_client.connect(m_conn); + if (ptr->get_state() != websocketpp::session::state::open) + std::cout << ptr->get_state() << " websocketpp::session::state " << std::endl; + // Start the ASIO io_service run loop + // this will cause a single connection to be made to the server. c.run() + // will exit when this connection is closed. + std::cout << "3 " << ptr << " " << m_conn_tls << std::endl; + this->m_status = WebsocketClient::CONNECTING; + while ((this->m_status != WebsocketClient::FAIL) && + (this->m_status != WebsocketClient::CLOSED) + && (this->m_status != WebsocketClient::STOP) + ) + { + try { + // while(this->m_status == WebsocketClient::CONNECTED){ + int count_of_handler = this->m_client.run(); + // std::cout<<"count_of_handler: " << count_of_handler<m_on_connected = on_connected; return 0; diff --git a/test/src/websocket_bench/websocket_client.h b/test/src/websocket_bench/websocket_client.h index 7ac0950..0b33a75 100644 --- a/test/src/websocket_bench/websocket_client.h +++ b/test/src/websocket_bench/websocket_client.h @@ -52,6 +52,7 @@ public: std::string Url(); WebsocketClient(bool tls); WebsocketClient(std::string url,bool tls); + int Connect(std::string); ~WebsocketClient(); void Close(); int SendMsg(const char * str,uint32_t len,websocketpp::frame::opcode::value);