no message

This commit is contained in:
zcy 2022-03-09 17:12:36 +08:00
parent e915fb1447
commit 3a3887a763
3 changed files with 257 additions and 236 deletions

View File

@ -130,7 +130,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
// delete mRightShow; // delete mRightShow;
mRightShow = nullptr; mRightShow = nullptr;
} }
if (uMsg == WM_USER_TCP_SERVER_CLOSE) { if (uMsg == WM_USER_TCP_SERVER_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str()); wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false); mRightShow->SetVisible(false);
@ -154,7 +153,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
if (uMsg == WM_USER_POS_CHANGED) { if (uMsg == WM_USER_POS_CHANGED) {
} }
if (uMsg == WM_ADD_TCPCLIENT_MONITOR) { if (uMsg == WM_ADD_TCPCLIENT_MONITOR) {
TcpClientInfo* info = (TcpClientInfo*)wParam; TcpClientInfo* info = (TcpClientInfo*)wParam;
TcpClientLibevent* cli = (TcpClientLibevent*)lParam; TcpClientLibevent* cli = (TcpClientLibevent*)lParam;
@ -212,7 +210,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
if (mWebsocketClientForm.find(info->ip) == mWebsocketClientForm.end()) if (mWebsocketClientForm.find(info->ip) == mWebsocketClientForm.end())
{ {
WebsocketClient* client = (WebsocketClient*)lParam; WebsocketClient* client = (WebsocketClient*)lParam;
auto form = new WebsocketClientForm(this, wstring2string(info->ip),client); auto form = new WebsocketClientForm(this, wstring2string(info->ip),client);
form->SetChildLayoutXML(L"basic/tcp_server_form.xml"); form->SetChildLayoutXML(L"basic/tcp_server_form.xml");
form->SetName(key); form->SetName(key);
@ -226,10 +223,28 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
if (ui::EventType::kEventSelect == ev->Type) { if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str()); wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
}
WebsocketClientForm* p = mWebsocketClientForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
wprintf(L"%s", p->GetName());
mRightSide->SelectItem(p->GetName());
}
else {
wprintf(L"%s", p->GetName());
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
} }
return true; return true;
} }
);
} }
if (uMsg == WM_ADD_TCPSERVER_MONITOR) { if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerInfo* info = (TcpServerInfo*)wParam;
@ -427,7 +442,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
} }
else { else {
p->SetVisible(true); p->SetVisible(true);
mRightSide->SelectItem(p->GetName()); mRightSide->SelectItem(p->GetName());
mRightShow = p; mRightShow = p;
} }
@ -489,15 +503,12 @@ void BasicForm::InitWindow(){
node->SetFixedHeight(30); node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"udp group")); node->SetText(nbase::StringPrintf(L"udp group"));
node->SetClass(L"list_topitem"); node->SetClass(L"list_topitem");
node->SetFixedHeight(30); node->SetFixedHeight(30);
mMonitor->GetRootNode()->AddChildNode(node); mMonitor->GetRootNode()->AddChildNode(node);
node = new ui::TreeNode; node = new ui::TreeNode;
node->SetText(nbase::StringPrintf(L"websocket client")); node->SetText(nbase::StringPrintf(L"websocket client"));
node->SetClass(L"list_topitem"); node->SetClass(L"list_topitem");

View File

@ -11,245 +11,256 @@ void on_message(WebsocketClient* c, websocketpp::connection_hdl hdl, message_ptr
if (ec) { if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl; std::cout << "Echo failed because: " << ec.message() << std::endl;
} }
if (c->m_onread != nullptr) { if (c->m_onread != nullptr) {
c->m_onread(c,msg->get_payload()); c->m_onread(c, msg->get_payload());
} }
} }
int WebsocketClient::SendMsg(const char * str,uint32_t len, int WebsocketClient::SendMsg(const char* str, uint32_t len,
websocketpp::frame::opcode::value opcode) websocketpp::frame::opcode::value opcode)
{ {
if(this->m_status != WebsocketClient::CONNECTED) if (this->m_status != WebsocketClient::CONNECTED)
return -1; return -1;
if(m_tls){ if (m_tls) {
websocketpp::lib::error_code ec; websocketpp::lib::error_code ec;
this->m_client_tls.send(m_conn_tls,str,len,opcode,ec); this->m_client_tls.send(m_conn_tls, str, len, opcode, ec);
if (ec) { if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl; std::cout << "Echo failed because: " << ec.message() << std::endl;
return -1; return -1;
} }
}else{ }
websocketpp::lib::error_code ec; else {
this->m_client.send(m_conn,str,len,opcode,ec); websocketpp::lib::error_code ec;
if (ec) { this->m_client.send(m_conn, str, len, opcode, ec);
std::cout << "Echo failed because: " << ec.message() << std::endl; if (ec) {
return -1; std::cout << "Echo failed because: " << ec.message() << std::endl;
} return -1;
} }
}
return 0; return 0;
} }
void on_open(WebsocketClient * c, websocketpp::connection_hdl hdl) { void on_open(WebsocketClient* c, websocketpp::connection_hdl hdl) {
if(c->m_tls){ if (c->m_tls) {
TlsClient::connection_ptr con = c->m_client_tls.get_con_from_hdl(hdl); TlsClient::connection_ptr con = c->m_client_tls.get_con_from_hdl(hdl);
uint32_t usocket = con->get_raw_socket().native_handle(); uint32_t usocket = con->get_raw_socket().native_handle();
auto m_server = con->get_response_header("Server"); auto m_server = con->get_response_header("Server");
std::cout<<"open from server"<<m_server<<std::endl; std::cout << "open from server" << m_server << std::endl;
c->m_status = WebsocketClient::CONNECTED; c->m_status = WebsocketClient::CONNECTED;
if (c->m_on_connected != nullptr) { if (c->m_on_connected != nullptr) {
c->m_on_connected(c); c->m_on_connected(c);
} }
}else{ }
Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl); else {
uint32_t usocket = con->get_raw_socket().native_handle(); Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl);
auto m_server = con->get_response_header("Server"); uint32_t usocket = con->get_raw_socket().native_handle();
std::cout<<"open from server"<<m_server<<std::endl; auto m_server = con->get_response_header("Server");
c->m_status = WebsocketClient::CONNECTED; std::cout << "open from server" << m_server << std::endl;
if (c->m_on_connected != nullptr) { c->m_status = WebsocketClient::CONNECTED;
c->m_on_connected(c); if (c->m_on_connected != nullptr) {
} c->m_on_connected(c);
} }
}
} }
void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl) { void on_close(WebsocketClient* c, websocketpp::connection_hdl hdl) {
// m_status = "Open"; // m_status = "Open";
// client::connection_ptr con = c->get_con_from_hdl(hdl); // client::connection_ptr con = c->get_con_from_hdl(hdl);
// m_server = con->get_response_header("Server"); // m_server = con->get_response_header("Server");
c->m_status = WebsocketClient::CLOSED; c->m_status = WebsocketClient::CLOSED;
std::cout<<"on_close"<<std::endl; std::cout << "on_close" << std::endl;
if (c->m_on_disconnected != nullptr) { if (c->m_on_disconnected != nullptr) {
c->m_on_disconnected(c,WebsocketClient::CloseReason::LOCAL_CLOSED); c->m_on_disconnected(c, WebsocketClient::CloseReason::LOCAL_CLOSED);
} }
} }
void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl) { void on_fail(WebsocketClient* c, websocketpp::connection_hdl hdl) {
std::cout<<"on_fail"<<std::endl; std::cout << "on_fail" << std::endl;
Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl); Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl);
auto state = con->get_state(); auto state = con->get_state();
if(state == websocketpp::session::state::closed) if (state == websocketpp::session::state::closed)
std::cout<<state <<" on_fail "<<std::endl; std::cout << state << " on_fail " << std::endl;
c->m_status = WebsocketClient::FAIL; c->m_status = WebsocketClient::FAIL;
if (c->m_on_disconnected != nullptr) { if (c->m_on_disconnected != nullptr) {
c->m_on_disconnected(c,WebsocketClient::CloseReason::PEER_CLOSED); c->m_on_disconnected(c, WebsocketClient::CloseReason::PEER_CLOSED);
} }
} }
WebsocketClient::~WebsocketClient(){ WebsocketClient::~WebsocketClient() {
this->m_status = WebsocketClient::STOP; this->m_status = WebsocketClient::STOP;
m_client.stop(); m_client.stop();
m_thread->join(); m_thread->join();
} }
WebsocketClient::WebsocketClient(std::string url,bool tls) WebsocketClient::WebsocketClient(std::string url, bool tls)
{ {
m_tls = tls; m_tls = tls;
m_auto_reconn = false; m_auto_reconn = false;
m_url = url; m_url = url;
if(m_tls){ this->m_status = WebsocketClient::CONNECTING;
// 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>(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);
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
));
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
this->m_status = Status::FAIL;
}
std::cout<<"2"<<std::endl;
// Note that connect here only requests a connection. No network messages are if (m_tls) {
// exchanged until the event loop starts running in the next line. // Set logging to be pretty verbose (everything except message payloads)
TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls); m_client_tls.set_access_channels(websocketpp::log::alevel::all);
if(ptr->get_state() != websocketpp::session::state::open) m_client_tls.clear_access_channels(websocketpp::log::alevel::frame_payload);
std::cout<<ptr->get_state() <<" websocketpp::session::state "<<std::endl; m_client_tls.set_tls_init_handler([this](websocketpp::connection_hdl) {
// Start the ASIO io_service run loop return websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::tlsv1);
// this will cause a single connection to be made to the server. c.run() });
// will exit when this connection is closed. // Initialize ASIO
std::cout<<"3 " << ptr << " " <<m_conn_tls <<std::endl; m_client_tls.init_asio();
this->m_status = WebsocketClient::CONNECTING; m_thread = new std::thread([this]()
while((this->m_status != WebsocketClient::FAIL) && {
(this->m_status != WebsocketClient::CLOSED) while (this->m_status != STOP)
&&(this->m_status != WebsocketClient::STOP) {
){ m_client_tls.set_message_handler(bind(&on_message, this, ::_1, ::_2));
try{ websocketpp::lib::error_code ec;
// while(this->m_status == WebsocketClient::CONNECTED){ std::cout << "1" << std::endl;
int count_of_handler = this->m_client_tls.run(); m_conn_tls = m_client_tls.get_connection(this->m_url, ec);
// std::cout<<"count_of_handler: " << count_of_handler<<std::endl; m_conn_tls->set_open_handler(websocketpp::lib::bind(
// } &on_open,
// run应该只执行一次就会退出 this,
}catch(std::exception e){ websocketpp::lib::placeholders::_1
std::cout<< "run exception"<< e.what(); ));
} m_conn_tls->set_fail_handler(websocketpp::lib::bind(
} &on_fail,
Sleep(1000); this,
} websocketpp::lib::placeholders::_1
std::cout<<"close"; ));
this->m_on_disconnected(this,WebsocketClient::CloseReason::LOCAL_CLOSED); m_conn_tls->set_close_handler(websocketpp::lib::bind(
}); &on_close,
} this,
else{ websocketpp::lib::placeholders::_1
// Set logging to be pretty verbose (everything except message payloads) ));
m_client.set_access_channels(websocketpp::log::alevel::all); if (ec) {
m_client.clear_access_channels(websocketpp::log::alevel::frame_payload); std::cout << "could not create connection because: " << ec.message() << std::endl;
this->m_status = Status::FAIL;
}
std::cout << "2" << std::endl;
// Initialize ASIO // Note that connect here only requests a connection. No network messages are
this->m_client.init_asio(); // exchanged until the event loop starts running in the next line.
m_thread = new std::thread([this]() TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls);
{ if (ptr->get_state() != websocketpp::session::state::open)
while (this->m_status != STOP) std::cout << ptr->get_state() << " websocketpp::session::state " << std::endl;
{ // Start the ASIO io_service run loop
this->m_client.set_message_handler(bind(&on_message, this, ::_1, ::_2)); // this will cause a single connection to be made to the server. c.run()
websocketpp::lib::error_code ec; // will exit when this connection is closed.
std::cout<<"1"<<std::endl; std::cout << "3 " << ptr << " " << m_conn_tls << std::endl;
m_conn = m_client.get_connection(this->m_url, ec); this->m_status = WebsocketClient::CONNECTING;
m_conn->set_open_handler(websocketpp::lib::bind( while ((this->m_status != WebsocketClient::FAIL) &&
&on_open, (this->m_status != WebsocketClient::CLOSED)
this, && (this->m_status != WebsocketClient::STOP)
websocketpp::lib::placeholders::_1 ) {
)); try {
m_conn->set_fail_handler(websocketpp::lib::bind( // while(this->m_status == WebsocketClient::CONNECTED){
&on_fail, int count_of_handler = this->m_client_tls.run();
this, // std::cout<<"count_of_handler: " << count_of_handler<<std::endl;
websocketpp::lib::placeholders::_1 // }
)); // run应该只执行一次就会退出
m_conn->set_close_handler(websocketpp::lib::bind( }
&on_close, catch (std::exception e) {
this, std::cout << "run exception" << e.what();
websocketpp::lib::placeholders::_1 }
)); }
if (ec) { Sleep(1000);
std::cout << "could not create connection because: " << ec.message() << std::endl; }
this->m_status = Status::FAIL; std::cout << "close";
} this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED);
std::cout<<"2"<<std::endl; });
}
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);
// Note that connect here only requests a connection. No network messages are // Initialize ASIO
// exchanged until the event loop starts running in the next line. this->m_client.init_asio();
Client::connection_ptr ptr = this->m_client.connect(m_conn); m_thread = new std::thread([this]()
if(ptr->get_state() != websocketpp::session::state::open) {
std::cout<<ptr->get_state() <<" websocketpp::session::state "<<std::endl; while (this->m_status != STOP)
// Start the ASIO io_service run loop {
// this will cause a single connection to be made to the server. c.run() this->m_client.set_message_handler(bind(&on_message, this, ::_1, ::_2));
// will exit when this connection is closed. websocketpp::lib::error_code ec;
std::cout<<"3 " << ptr << " " <<m_conn_tls <<std::endl; std::cout << "1" << std::endl;
this->m_status = WebsocketClient::CONNECTING; m_conn = m_client.get_connection(this->m_url, ec);
while((this->m_status != WebsocketClient::FAIL) && if (m_conn != nullptr) {
(this->m_status != WebsocketClient::CLOSED) m_conn->set_open_handler(websocketpp::lib::bind(
&&(this->m_status != WebsocketClient::STOP) &on_open,
){ this,
try{ websocketpp::lib::placeholders::_1
// while(this->m_status == WebsocketClient::CONNECTED){ ));
int count_of_handler = this->m_client.run(); m_conn->set_fail_handler(websocketpp::lib::bind(
// std::cout<<"count_of_handler: " << count_of_handler<<std::endl; &on_fail,
// } this,
// run应该只执行一次就会退出 websocketpp::lib::placeholders::_1
}catch(std::exception e){ ));
std::cout<< "run exception"<< e.what(); m_conn->set_close_handler(websocketpp::lib::bind(
} &on_close,
} this,
Sleep(1000); websocketpp::lib::placeholders::_1
} ));
std::cout<<"close"; }
this->m_on_disconnected(this,WebsocketClient::CloseReason::LOCAL_CLOSED); 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;
}
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.
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<<std::endl;
// }
// run应该只执行一次就会退出
}
catch (std::exception e) {
std::cout << "run exception" << e.what();
}
}
Sleep(1000);
}
std::cout << "close";
if(m_on_disconnected)
m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED);
});
}
} }
int WebsocketClient::SetOnConnectedHandler(OnConnectedHandler on_connected){ int WebsocketClient::SetOnConnectedHandler(OnConnectedHandler on_connected) {
this->m_on_connected = on_connected; this->m_on_connected = on_connected;
return 0; return 0;
} }
int WebsocketClient::SetOnDisConnectedHandler(OnDisConnectedHandler on_disconnected){ int WebsocketClient::SetOnDisConnectedHandler(OnDisConnectedHandler on_disconnected) {
this->m_on_disconnected = on_disconnected; this->m_on_disconnected = on_disconnected;
return 0; return 0;
} }
int WebsocketClient::SetOnReadHandler(OnReadHandler onread){ int WebsocketClient::SetOnReadHandler(OnReadHandler onread) {
this->m_onread = onread; this->m_onread = onread;
return 0; return 0;
} }

View File

@ -1,4 +1,5 @@
#include "websocket_client_form.h" #include "websocket_client_form.h"
#include "utils.h"
WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, WebsocketClient* p) WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, WebsocketClient* p)
{ {
@ -28,26 +29,24 @@ void WebsocketClientForm::Init()
m_btn_close_form = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart")); m_btn_close_form = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
if (nullptr != m_btn_close_form) if (nullptr != m_btn_close_form)
m_btn_close_form->AttachClick([this](const ui::EventArgs *ev) { m_btn_close_form->AttachClick([this](const ui::EventArgs* ev) {
return true;
}
);
return true;
}
);
if(nullptr != m_btn_save_lua) if(nullptr != m_btn_save_lua)
m_btn_save_lua->AttachClick( m_btn_save_lua->AttachClick(
[this](ui::EventArgs* ev) [this](ui::EventArgs* ev){
{
return true; return true;
} }
); );
if (nullptr != m_btn_send_data) { if (nullptr != m_btn_send_data) {
m_btn_send_data->AttachClick([this](const ui::EventArgs* ev) { m_btn_send_data->AttachClick([this](const ui::EventArgs* ev) {
return true; return true;
}); });
} }
} }
@ -58,5 +57,5 @@ LuaDelegate* WebsocketClientForm::LuaVM()
void WebsocketClientForm::ShowDataInEdit(const char*src) void WebsocketClientForm::ShowDataInEdit(const char*src)
{ {
m_rich_edit_1->AppendText(string2wstring(std::string(src)),false);
} }