107 lines
3.3 KiB
C++
107 lines
3.3 KiB
C++
#include "websocket_server_form.h"
|
||
#include "utils.h"
|
||
|
||
#define WEBSOCKET_SERVER_LUA "websocket_server.lua"
|
||
|
||
WebsocketServerForm::WebsocketServerForm(ui::Window* hwnd, string url,
|
||
WebsocketServer* server)
|
||
{
|
||
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);
|
||
}
|
||
|
||
void WebsocketServerForm::Init()
|
||
{
|
||
ui::ChildBox::Init();
|
||
|
||
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));
|
||
|
||
|
||
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);
|
||
});
|
||
}
|
||
|
||
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"));
|
||
|
||
if (nullptr != m_btn_close_form) {
|
||
m_btn_close_form->AttachClick([this](ui::EventArgs *ev) {
|
||
|
||
return true;
|
||
});
|
||
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;
|
||
}
|
||
);
|
||
}
|
||
|
||
LuaDelegate* WebsocketServerForm::LuaVM()
|
||
{
|
||
return mLua;
|
||
}
|
||
|
||
void WebsocketServerForm::ShowDataInEdit(const char*src)
|
||
{
|
||
m_rich_edit_1->AppendText(string2wstring(std::string(src)), false);
|
||
}
|