add websocket server

This commit is contained in:
zcy 2022-03-13 01:50:44 +08:00
parent 4fcdd48a6b
commit 1572832de6
14 changed files with 212 additions and 38 deletions

View File

@ -29,7 +29,7 @@
<Control width="stretch"/> <Control width="stretch"/>
</HBox> </HBox>
<HBox width="stretch" height="35" margin="10,0,0,0"> <HBox width="stretch" height="35" margin="10,0,0,0">
<Label name="title" text="端口:" valign="bottom" margin="30,0,0,0" width="70" height="30" font="arial_14"/> <Label name="title_port" text="端口:" valign="bottom" margin="30,0,0,0" width="70" height="30" font="arial_14"/>
<RichEdit class="simple input" width="90" name="port_input" height="30" margin="0,3" padding="6,6,6" /> <RichEdit class="simple input" width="90" name="port_input" height="30" margin="0,3" padding="6,6,6" />
<Control width="stretch"/> <Control width="stretch"/>
</HBox> </HBox>

View File

@ -112,6 +112,26 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
delete mRightShow; delete mRightShow;
mRightShow = nullptr; mRightShow = nullptr;
} }
if (uMsg == WM_ADD_WEBSOCKET_CLIENT_CLOSE) {
wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
mRightShow->SetVisible(false);
mRightSide->SetAutoDestroy(true);
mRightSide->RemoveAll();
mWebsocketClientForm.erase(*(wstring*)(wParam));
int cnt = mMonitor->GetRootNode()->GetChildNode(5)->GetChildNodeCount();
ui::TreeNode* p = nullptr;
for (int i = 0; i < cnt; i++) {
if (mMonitor->GetRootNode()->GetChildNode(5)->GetChildNode(i)->GetText()
== *(wstring*)(wParam)) {
p = mMonitor->GetRootNode()->GetChildNode(5)->GetChildNode(i);
break;
}
}
mMonitor->GetRootNode()->GetChildNode(5)->RemoveChildNode(p);
// delete mRightShow;
mRightShow = nullptr;
}
if (uMsg == WM_USER_TCP_CLIENT_CLOSE) { if (uMsg == WM_USER_TCP_CLIENT_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);
@ -221,7 +241,7 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
client->SetOnReadHandler(std::bind(&WebsocketClientForm::OnReadHandler, client->SetOnReadHandler(std::bind(&WebsocketClientForm::OnReadHandler,
form, std::placeholders::_1, std::placeholders::_2)); form, std::placeholders::_1, std::placeholders::_2));
form->SetChildLayoutXML(L"basic/tcp_server_form.xml"); form->SetChildLayoutXML(L"basic/websocket_client_form.xml");
form->SetName(key); form->SetName(key);
form->SetVisible(false); form->SetVisible(false);
mWebsocketClientForm[key] = form; mWebsocketClientForm[key] = form;
@ -256,6 +276,57 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
} }
); );
} }
if (uMsg == WM_ADD_WEBSOCKET_SERVER) {
TcpServerInfo* info = (TcpServerInfo*)wParam;
ui::TreeNode* node = new ui::TreeNode;
mMonitor->GetRootNode()->GetChildNode(5)->AddChildNode(node);
auto key = info->ip;
node->SetText(key);
node->SetClass(L"listitem");
if (mWebsocketServerForm.find(info->ip) == mWebsocketServerForm.end())
{
WebsocketServer* server = (WebsocketServer*)lParam;
auto form = new WebsocketServerForm(this, wstring2string(info->ip), server);
form->SetChildLayoutXML(L"basic/tcp_server_form.xml");
form->SetName(key);
form->SetVisible(false);
mWebsocketServerForm[key] = form;
if (!mRightSide->Add(form))
printf("error 1");
}
node->AttachAllEvents(
[this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
}
WebsocketServerForm* p = mWebsocketServerForm[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;
}
);
}
if (uMsg == WM_ADD_TCPSERVER_MONITOR) { if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerInfo* info = (TcpServerInfo*)wParam;
TcpServerLibevent* server = (TcpServerLibevent*)lParam; TcpServerLibevent* server = (TcpServerLibevent*)lParam;
@ -485,6 +556,7 @@ void BasicForm::InitWindow(){
return true; return true;
}); });
} }
mMonitor = dynamic_cast<ui::TreeView*>(FindControl(L"tree")); mMonitor = dynamic_cast<ui::TreeView*>(FindControl(L"tree"));
mMonitor->SetClass(L"list"); mMonitor->SetClass(L"list");
mMonitor->SetIndent(5); mMonitor->SetIndent(5);
@ -573,6 +645,11 @@ LuaBindInterface* BasicForm::FindCurrentFormByLuaPointer(lua_State* pointer)
return (LuaBindInterface*)itr->second; return (LuaBindInterface*)itr->second;
} }
} }
for (auto itr = mWebsocketClientForm.begin(); itr != mWebsocketClientForm.end(); itr++) {
if (itr->second->LuaVM()->VM() == pointer) {
return (LuaBindInterface*)itr->second;
}
}
return nullptr; return nullptr;
} }

View File

@ -21,6 +21,7 @@
#include "udp_group_form.h" #include "udp_group_form.h"
#include "websocket_client_form.h" #include "websocket_client_form.h"
#include "websocket_client.h" #include "websocket_client.h"
#include "websocket_server_form.h"
#include <vector> #include <vector>
#include <map> #include <map>
@ -64,6 +65,7 @@ private:
std::map<std::wstring, UdpForm*> mUdpForm; std::map<std::wstring, UdpForm*> mUdpForm;
std::map<std::wstring, UdpGroupForm*> mUdpGroupForm; std::map<std::wstring, UdpGroupForm*> mUdpGroupForm;
std::map<std::wstring, WebsocketClientForm*> mWebsocketClientForm; std::map<std::wstring, WebsocketClientForm*> mWebsocketClientForm;
std::map<std::wstring, WebsocketServerForm*> mWebsocketServerForm;
ui::Control* mRightShow; ui::Control* mRightShow;
}; };

View File

@ -93,7 +93,8 @@ void MainThread::Init()
#endif #endif
auto dpiManager = ui::DpiManager::GetInstance(); auto dpiManager = ui::DpiManager::GetInstance();
dpiManager->SetAdaptDPI(); // dpiManager->SetAdaptDPI();
dpiManager->SetScale(1);
// 创建一个默认带有阴影的居中窗口 // 创建一个默认带有阴影的居中窗口
gMainWindow = new BasicForm(); gMainWindow = new BasicForm();

View File

@ -11,6 +11,7 @@
#include "tcp_client.h" #include "tcp_client.h"
#include "udp_libevent.h" #include "udp_libevent.h"
#include "websocket_client.h" #include "websocket_client.h"
#include "websocket_server.h"
#include "utils.h" #include "utils.h"
#include <string> #include <string>
@ -217,7 +218,7 @@ void NewMonitorForm::InitWindow()
p->ip = m_ip_select->GetText(); p->ip = m_ip_select->GetText();
// p->port = port; // p->port = port;
// p->socket_fd = udp->SocketFD(); // p->socket_fd = udp->SocketFD();
printf("¿ªÆô·þÎñ¶Ë %d \r\n", p->socket_fd); printf("websocket ¿Í»§¶Ë %d \r\n", p->socket_fd);
auto succ = ::PostMessage(m_parent->GetHWND(), auto succ = ::PostMessage(m_parent->GetHWND(),
WM_ADD_WEBSOCKET_CLIENT, (WPARAM)p, (LPARAM)wsclient); WM_ADD_WEBSOCKET_CLIENT, (WPARAM)p, (LPARAM)wsclient);
@ -225,6 +226,25 @@ void NewMonitorForm::InitWindow()
printf("postmessage error :%d\r\n", GetLastError()); printf("postmessage error :%d\r\n", GetLastError());
} }
} }
if (m_combo_type->GetText() == L"websocket server") {
wprintf(L"%s\r\n", m_ip_select->GetText().c_str());
wprintf(L"%s\r\n", m_port_select->GetText().c_str());
int port = atoi(wstring2string(m_port_select->GetText()).c_str());
WebsocketServer* wsserver = new
WebsocketServer(wstring2string(m_ip_select->GetText().c_str()), atoi(wstring2string(m_port_select->GetText()).c_str()));
TcpServerInfo* p = new TcpServerInfo;
p->ip = m_ip_select->GetText();
p->port = port;
// p->socket_fd = udp->SocketFD();
printf("websocket ·þÎñ¶Ë %d \r\n", port);
auto succ = ::PostMessage(m_parent->GetHWND(),
WM_ADD_WEBSOCKET_SERVER, (WPARAM)p, (LPARAM)wsserver);
if (!succ) {
printf("postmessage error :%d\r\n", GetLastError());
}
}
return true; return true;
}); });
} }
@ -364,15 +384,23 @@ void NewMonitorForm::InitWindow()
} }
} }
if ((text == L"tcp client")|| (text == L"tcp server") || if ((text == L"tcp client")|| (text == L"tcp server") ||
(text == L"udp") || (text == L"udp group") || (text == L"websocket server") || (text == L"websocket client") (text == L"udp") || (text == L"udp group")) {
) {
this->m_ip_config_vbox->SetVisible(true); this->m_ip_config_vbox->SetVisible(true);
this->m_uart_config_vbox->SetVisible(false); this->m_uart_config_vbox->SetVisible(false);
m_port_select->SetVisible(true);
m_ip_select->SetText(L"127.0.0.1"); m_ip_select->SetText(L"127.0.0.1");
m_port_select->SetText(L"9001"); m_port_select->SetText(L"9001");
} }
if ( (text == L"websocket server") || (text == L"websocket client")) {
this->m_ip_config_vbox->SetVisible(true);
this->m_uart_config_vbox->SetVisible(false);
m_port_select->SetVisible(false);
m_ip_select->SetText(L"wss://117.50.176.114:8080/echo");
m_port_select->SetText(L"9001");
}
} }
} }
return true; return true;

View File

@ -183,6 +183,7 @@
<ClCompile Include="websocket_client.cpp" /> <ClCompile Include="websocket_client.cpp" />
<ClCompile Include="websocket_client_form.cpp" /> <ClCompile Include="websocket_client_form.cpp" />
<ClCompile Include="websocket_server.cpp" /> <ClCompile Include="websocket_server.cpp" />
<ClCompile Include="websocket_server_form.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\cppnetwork\websocket_client.h" /> <ClInclude Include="..\..\..\cppnetwork\websocket_client.h" />
@ -204,6 +205,7 @@
<ClInclude Include="udp_libevent.h" /> <ClInclude Include="udp_libevent.h" />
<ClInclude Include="utils.h" /> <ClInclude Include="utils.h" />
<ClInclude Include="websocket_client_form.h" /> <ClInclude Include="websocket_client_form.h" />
<ClInclude Include="websocket_server_form.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml" /> <Xml Include="..\Debug\resources\themes\default\basic\basic.xml" />

View File

@ -75,6 +75,9 @@
<ClCompile Include="websocket_server.cpp"> <ClCompile Include="websocket_server.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="websocket_server_form.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -134,6 +137,9 @@
<ClInclude Include="..\..\..\cppnetwork\websocket_client.h"> <ClInclude Include="..\..\..\cppnetwork\websocket_client.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="websocket_server_form.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml"> <Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
@ -157,6 +163,9 @@
<Xml Include="..\x64\Debug\resources\themes\default\basic\udp_form.xml"> <Xml Include="..\x64\Debug\resources\themes\default\basic\udp_form.xml">
<Filter>资源文件</Filter> <Filter>资源文件</Filter>
</Xml> </Xml>
<Xml Include="..\x64\Debug\resources\themes\default\basic\websocket_client_form.xml">
<Filter>资源文件</Filter>
</Xml>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="proto_debuger.rc"> <ResourceCompile Include="proto_debuger.rc">

View File

@ -11,8 +11,8 @@ UdpGroupForm::UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataG
this->mUdpPeer = p; this->mUdpPeer = p;
this->m_url = url; this->m_url = url;
this->m_port = port; this->m_port = port;
mLua = new LuaDelegate;
mLua = new LuaDelegate;
mLuaFile.open(UDP_LUA_SCRIPT); mLuaFile.open(UDP_LUA_SCRIPT);
std::string lua_script; std::string lua_script;
if (mLuaFile.is_open()) { if (mLuaFile.is_open()) {
@ -24,20 +24,19 @@ UdpGroupForm::UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataG
} }
mLuaScript = lua_script; mLuaScript = lua_script;
mLua->DoString(lua_script); mLua->DoString(lua_script);
this->mLua->BindFunction("showdata", LuaShowData);
UdpDataGramLibevent::OnReadDataHandle onRecvCallBack UdpDataGramLibevent::OnReadDataHandle onRecvCallBack
= [this](const char* dat, int len, struct sockaddr_in sendarr) { = [this](const char* dat, int len, struct sockaddr_in sendarr) {
std::cout << "receive udp datagram len" << len << dat << "\r\n"; std::cout << "receive udp datagram len" << len << dat << "\r\n";
this->mLua->CallFuntion<std::string, std::string, LUA_INTEGER>("OnUdpData", std::string(dat), this->mLua->CallFuntion<std::string, std::string, LUA_INTEGER>("OnUdpData", std::string(dat),
inet_ntoa(sendarr.sin_addr), sendarr.sin_port); inet_ntoa(sendarr.sin_addr), sendarr.sin_port);
}; };
if (mUdpPeer != nullptr) { if (mUdpPeer != nullptr) {
std::cout << "\r\n" << &onRecvCallBack << "\r\n"; std::cout << "\r\n" << &onRecvCallBack << "\r\n";
mUdpPeer->SetOnReadHandle(onRecvCallBack); mUdpPeer->SetOnReadHandle(onRecvCallBack);
} }
this->mLua->BindFunction("showdata", LuaShowData);
std::cout << "lua script is " << lua_script << std::endl; std::cout << "lua script is " << lua_script << std::endl;
} }

View File

@ -45,15 +45,14 @@ private:
ui::Button* m_button_3; ui::Button* m_button_3;
ui::Button* m_button_4; ui::Button* m_button_4;
LuaDelegate* mLua;
std::string mLuaScript;
std::ifstream mLuaFile; std::ifstream mLuaFile;
std::ofstream mLuaFileEdit; std::ofstream mLuaFileEdit;
virtual void HandleMessage(ui::EventArgs& msg); virtual void HandleMessage(ui::EventArgs& msg);
std::string mLuaScript;
std::string m_url; std::string m_url;
int m_port; int m_port;
LuaDelegate* mLua;
UdpDataGramLibevent* mUdpPeer; UdpDataGramLibevent* mUdpPeer;
}; };

View File

@ -94,10 +94,18 @@ void on_fail(WebsocketClient* c, websocketpp::connection_hdl hdl) {
WebsocketClient::~WebsocketClient() { WebsocketClient::~WebsocketClient() {
this->m_status = WebsocketClient::STOP; this->m_status = WebsocketClient::STOP;
m_client.stop(); if(m_tls)
m_client_tls.stop();
else
m_client.stop();
m_thread->join(); m_thread->join();
} }
void WebsocketClient::Close()
{
this->m_status = STOP;
}
std::string WebsocketClient::Url() std::string WebsocketClient::Url()
{ {
return this->m_url; return this->m_url;
@ -126,6 +134,11 @@ WebsocketClient::WebsocketClient(std::string url, bool tls)
websocketpp::lib::error_code ec; websocketpp::lib::error_code ec;
std::cout << "1" << std::endl; std::cout << "1" << std::endl;
m_conn_tls = m_client_tls.get_connection(this->m_url, ec); 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( m_conn_tls->set_open_handler(websocketpp::lib::bind(
&on_open, &on_open,
this, this,
@ -141,11 +154,7 @@ WebsocketClient::WebsocketClient(std::string url, bool tls)
this, this,
websocketpp::lib::placeholders::_1 websocketpp::lib::placeholders::_1
)); ));
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
this->m_status = Status::FAIL;
break;
}
std::cout << "2" << std::endl; std::cout << "2" << std::endl;
// Note that connect here only requests a connection. No network messages are // Note that connect here only requests a connection. No network messages are
// exchanged until the event loop starts running in the next line. // exchanged until the event loop starts running in the next line.
@ -163,7 +172,8 @@ WebsocketClient::WebsocketClient(std::string url, bool tls)
) { ) {
try { try {
int count_of_handler = this->m_client_tls.run(); int count_of_handler = this->m_client_tls.run();
// } std::cout << "4 " << std::endl;
// run应该只执行一次就会退出 // run应该只执行一次就会退出
} }
catch (std::exception e) { catch (std::exception e) {
@ -173,7 +183,8 @@ WebsocketClient::WebsocketClient(std::string url, bool tls)
Sleep(1000); Sleep(1000);
} }
std::cout << "close"; std::cout << "close";
this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); if(m_on_disconnected)
this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED);
}); });
} }
else { else {

View File

@ -52,6 +52,7 @@ public:
std::string Url(); std::string Url();
WebsocketClient(std::string url,bool tls); WebsocketClient(std::string url,bool tls);
~WebsocketClient(); ~WebsocketClient();
void Close();
int SendMsg(const char * str,uint32_t len,websocketpp::frame::opcode::value); int SendMsg(const char * str,uint32_t len,websocketpp::frame::opcode::value);
friend void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl); friend void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl);
friend void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl); friend void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl);

View File

@ -1,5 +1,10 @@
#include "websocket_client_form.h" #include "websocket_client_form.h"
#include "utils.h" #include "utils.h"
#include "lua_bind.h"
#define WEBSOCKET_CLIENT_LUA "websocket_client.lua"
WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, WebsocketClient* p) WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, WebsocketClient* p)
{ {
@ -8,10 +13,26 @@ WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, Websocket
} }
mWebsocketClient = p; mWebsocketClient = p;
mLua = new LuaDelegate;
mLuaFile.open(WEBSOCKET_CLIENT_LUA);
std::string lua_script;
if (mLuaFile.is_open()) {
std::string s;
while (getline(mLuaFile, s)) {
lua_script += s + "\r\n";
}
mLuaFile.close();
}
mLuaScript = lua_script;
mLua->DoString(lua_script);
this->mLua->BindFunction("showdata", LuaShowData);
} }
WebsocketClientForm::~WebsocketClientForm() WebsocketClientForm::~WebsocketClientForm()
{ {
std::cout << "~WebsocketClientForm"<<std::endl;
delete mWebsocketClient;
} }
@ -19,9 +40,15 @@ void WebsocketClientForm::Init()
{ {
ui::ChildBox::Init(); ui::ChildBox::Init();
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info")); m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt")); m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_recv_edit"));
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script")); m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script_edit"));
m_uart_send_edit = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
m_rich_edit_2->SetRich(true);
m_rich_edit_2->SetReturnMsgWantCtrl(true);
m_rich_edit_2->SetText(string2wstring(mLuaScript));
m_websocket_send_edit = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_send_edit"));
m_btn_send_data = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data")); m_btn_send_data = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send")); m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send")); m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
@ -39,6 +66,7 @@ void WebsocketClientForm::Init()
string2wstring(mWebsocketClient->Url()) + L"Á¬½Óʧ°Ü"); string2wstring(mWebsocketClient->Url()) + L"Á¬½Óʧ°Ü");
} }
} }
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) {
wstring* name = new wstring(this->GetName()); wstring* name = new wstring(this->GetName());
@ -51,14 +79,31 @@ void WebsocketClientForm::Init()
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){
std::cout << "±£´ælua½Å±¾\r\n";
return true; std::string lua = wstring2string(this->m_rich_edit_2->GetText());
if (0 == this->mLua->UpdateScript(lua)) {
this->mLuaScript = lua;
mLuaFileEdit = std::ofstream(WEBSOCKET_CLIENT_LUA, std::ios::out | std::ios::trunc);
mLuaFileEdit.write(lua.c_str(), lua.size());
mLuaFileEdit.flush();
mLuaFileEdit.close();
}
else {
MessageBox(0, L"lua½Å±¾´íÎó", L"", 0);
return true;
}
std::cout << lua.c_str() << "\r\n";
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) {
this->mWebsocketClient->SendMsg(
wstring2string(m_websocket_send_edit->GetText()).c_str(),
wstring2string(m_websocket_send_edit->GetText()).size(),
websocketpp::frame::opcode::TEXT);
m_websocket_send_edit->SetText(std::wstring(L""));
return true; return true;
}); });
} }
@ -66,7 +111,7 @@ void WebsocketClientForm::Init()
LuaDelegate* WebsocketClientForm::LuaVM() LuaDelegate* WebsocketClientForm::LuaVM()
{ {
return nullptr; return mLua;
} }
void WebsocketClientForm::ShowDataInEdit(const char*src) void WebsocketClientForm::ShowDataInEdit(const char*src)
@ -96,4 +141,5 @@ void WebsocketClientForm::OnDisConnected(WebsocketClient* ws, WebsocketClient::
void WebsocketClientForm::OnReadHandler(WebsocketClient* ws, std::string message) void WebsocketClientForm::OnReadHandler(WebsocketClient* ws, std::string message)
{ {
std::cout << "recv data from hanlder" << message.c_str(); std::cout << "recv data from hanlder" << message.c_str();
this->mLua->CallFuntion<std::string>("OnWebSocketData", message);
} }

View File

@ -15,6 +15,7 @@
#include "udp_libevent.h" #include "udp_libevent.h"
#include <istream> #include <istream>
#include "websocket_client.h" #include "websocket_client.h"
#include "lua_bind.h"
class WebsocketClientForm : class WebsocketClientForm :
public ui::ChildBox, public ui::ChildBox,
@ -33,7 +34,7 @@ private:
ui::Label* m_label_1; ui::Label* m_label_1;
ui::RichEdit* m_rich_edit_1; ui::RichEdit* m_rich_edit_1;
ui::RichEdit* m_rich_edit_2; ui::RichEdit* m_rich_edit_2;
ui::RichEdit* m_uart_send_edit; ui::RichEdit* m_websocket_send_edit;
ui::Button* m_btn_send_data; ui::Button* m_btn_send_data;
ui::Button* m_btn_save_lua; ui::Button* m_btn_save_lua;
ui::Button* m_btn_close_form; ui::Button* m_btn_close_form;
@ -41,8 +42,15 @@ private:
ui::CheckBox* m_check_box_2; ui::CheckBox* m_check_box_2;
ui::CheckBox* m_check_box_3; ui::CheckBox* m_check_box_3;
ui::CheckBox* m_check_box_4; ui::CheckBox* m_check_box_4;
WebsocketClient* mWebsocketClient; WebsocketClient* mWebsocketClient;
std::fstream mLuaFileRead; std::fstream mLuaFileRead;
std::ofstream mLuaFileEdit; std::ofstream mLuaFileEdit;
LuaDelegate* mLua;
std::string mLuaScript;
std::ifstream mLuaFile;
}; };

View File

@ -1,12 +1,3 @@
/*
* @Author: your name
* @Date: 2022-02-28 21:03:56
* @LastEditTime: 2022-02-28 23:24:06
* @LastEditors: Please set LastEditors
* @Description: koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \test\websocket_server.h
*/
#pragma once #pragma once
#include <websocketpp/config/asio_no_tls.hpp> #include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp> #include <websocketpp/server.hpp>