2022-03-13 18:50:41 +08:00
|
|
|
|
#include "websocket_server_form.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
|
|
#define WEBSOCKET_SERVER_LUA "websocket_server.lua"
|
|
|
|
|
|
2022-03-13 21:41:02 +08:00
|
|
|
|
WebsocketServerForm::WebsocketServerForm(ui::Window* hwnd, string url,
|
2023-01-20 00:17:17 +08:00
|
|
|
|
WebsocketServer* server) {
|
2022-03-13 18:50:41 +08:00
|
|
|
|
if (nullptr != hwnd) {
|
|
|
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
|
|
|
}
|
|
|
|
|
m_server = server;
|
|
|
|
|
|
|
|
|
|
mLua = new LuaDelegate;
|
|
|
|
|
mLuaFile.open(WEBSOCKET_SERVER_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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 00:17:17 +08:00
|
|
|
|
WebsocketServerForm::~WebsocketServerForm() {
|
2022-03-14 00:05:00 +08:00
|
|
|
|
std::cout << "~WebsocketServerForm\r\n";
|
|
|
|
|
std::cout << "~WebsocketServerForm\r\n";
|
2022-03-16 23:13:59 +08:00
|
|
|
|
delete m_server;
|
2022-03-14 00:05:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 00:17:17 +08:00
|
|
|
|
void WebsocketServerForm::Init() {
|
2022-03-13 18:50:41 +08:00
|
|
|
|
ui::ChildBox::Init();
|
2022-03-13 21:41:02 +08:00
|
|
|
|
|
2022-03-13 18:50:41 +08:00
|
|
|
|
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
|
|
|
|
|
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_edit"));
|
|
|
|
|
|
|
|
|
|
m_rich_edit_2->SetRich(true);
|
|
|
|
|
m_rich_edit_2->SetReturnMsgWantCtrl(true);
|
|
|
|
|
|
|
|
|
|
m_rich_edit_2->SetText(string2wstring(mLuaScript));
|
|
|
|
|
|
2022-03-13 21:41:02 +08:00
|
|
|
|
|
|
|
|
|
m_label_1->SetText(string2wstring(this->m_server->Url()) + L":"
|
|
|
|
|
+ std::to_wstring(m_server->Port()));
|
|
|
|
|
if (nullptr != m_server) {
|
|
|
|
|
m_server->SetOnConnectionCloseHanlder([this](std::string addr, uint32_t port) {
|
|
|
|
|
this->mLua->CallFuntion<std::string, lua_Integer>("OnWebsocketCloseConnection",
|
|
|
|
|
addr, int(port));
|
|
|
|
|
});
|
|
|
|
|
m_server->SetOnNewConnectionHanlder([this](std::string addr, uint32_t port) {
|
|
|
|
|
this->mLua->CallFuntion<std::string, lua_Integer>("OnWebsocketNewConnection",
|
|
|
|
|
addr, int(port));
|
|
|
|
|
});
|
|
|
|
|
m_server->SetOnMessageHanlder([this](std::string addr, uint32_t port, std::string message)
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"SetOnMessageHanlder\r\n";
|
|
|
|
|
std::cout << addr << port << message<<"\r\n";
|
|
|
|
|
this->mLua->CallFuntion<std::string, lua_Integer,std::string>("OnWebSocketDataServer", addr,int(port),message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 20:23:51 +08:00
|
|
|
|
|
2022-03-13 18:50:41 +08:00
|
|
|
|
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_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_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
|
|
|
|
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
|
|
|
|
m_btn_close_form = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
|
|
|
|
|
2022-03-13 21:41:02 +08:00
|
|
|
|
if (nullptr != m_btn_close_form) {
|
2022-03-14 00:05:00 +08:00
|
|
|
|
m_btn_close_form->AttachClick([this](ui::EventArgs* ev) {
|
|
|
|
|
wstring* name = new wstring(this->GetName());
|
|
|
|
|
::PostMessage(this->GetWindow()->GetHWND(),
|
|
|
|
|
WM_ADD_WEBSOCKET_SERVER_CLOSE, (WPARAM)name, 0);
|
2022-03-13 21:41:02 +08:00
|
|
|
|
return true;
|
2022-03-14 00:05:00 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2022-06-03 20:23:51 +08:00
|
|
|
|
|
|
|
|
|
if (nullptr != m_btn_send_data) {
|
|
|
|
|
m_btn_send_data->AttachClick([this](ui::EventArgs* ev) {
|
|
|
|
|
m_server->SendDataAllClient(wstring2string(m_websocket_send_edit->GetText()).c_str(),
|
|
|
|
|
strlen(wstring2string(m_websocket_send_edit->GetText()).c_str()),
|
|
|
|
|
websocketpp::frame::opcode::value::TEXT);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 18:50:41 +08:00
|
|
|
|
if (nullptr != m_btn_save_lua)
|
|
|
|
|
m_btn_save_lua->AttachClick(
|
|
|
|
|
[this](ui::EventArgs* ev) {
|
|
|
|
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD>ű<EFBFBD>\r\n";
|
|
|
|
|
std::string lua = wstring2string(this->m_rich_edit_2->GetText());
|
|
|
|
|
if (0 == this->mLua->UpdateScript(lua)) {
|
|
|
|
|
this->mLuaScript = lua;
|
|
|
|
|
mLuaFileEdit = std::ofstream(WEBSOCKET_SERVER_LUA,
|
|
|
|
|
std::ios::out | std::ios::trunc);
|
|
|
|
|
mLuaFileEdit.write(lua.c_str(), lua.size());
|
|
|
|
|
mLuaFileEdit.flush();
|
|
|
|
|
mLuaFileEdit.close();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", L"", 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
std::cout << lua.c_str() << "\r\n";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 00:17:17 +08:00
|
|
|
|
LuaDelegate* WebsocketServerForm::LuaVM() {
|
2022-03-13 18:50:41 +08:00
|
|
|
|
return mLua;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 00:17:17 +08:00
|
|
|
|
void WebsocketServerForm::ShowDataInEdit(const char*src) {
|
2022-03-13 18:50:41 +08:00
|
|
|
|
m_rich_edit_1->AppendText(string2wstring(std::string(src)), false);
|
|
|
|
|
}
|
2023-01-20 00:17:17 +08:00
|
|
|
|
|