X-Git-Url: https://gitweb.ps.run/autorec/blobdiff_plain/8d2244aefe172b5b1e15a32ad23991945d128f3a..ed07fa5aa45759ad962bd4dcef85d53b65070424:/src/ws.h diff --git a/src/ws.h b/src/ws.h index 82d7976..d3acb27 100644 --- a/src/ws.h +++ b/src/ws.h @@ -1,86 +1,29 @@ -// #define _WEBSOCKETPP_CPP11_INTERNAL_ -// #define ASIO_STANDALONE -// #include -// #include - -// using client = websocketpp::client; - #include "mongoose.h" #include "json.hpp" using json = nlohmann::json; #include +#include namespace ws { - // client c; - - // void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) { - // auto j = json::parse(msg->get_payload()); - // int op = j["op"].get(); - - // if (op == 0) { - // json response = { { "op", 1 }, - // { "d", { - // { "rpcVersion", 1 }, - // } } }; - // auto responseStr = response.dump(); - // websocketpp::lib::error_code ec; - // c.send(hdl, responseStr, websocketpp::frame::opcode::TEXT, ec); - // if (ec) - // MessageBoxA(NULL, ec.message().c_str(), "error", MB_OK); - // } else if (op == 2) { - // MessageBoxA(NULL, "hura", "connected", MB_OK); - // } - // } - - // void init() - // { - // c.set_access_channels(websocketpp::log::alevel::all); - // c.clear_access_channels(websocketpp::log::alevel::frame_payload); - // c.set_error_channels(websocketpp::log::elevel::all); - - // // Initialize ASIO - // c.init_asio(); - - // // Register our message handler - // c.set_message_handler(&on_message); - // } - - // void connect(std::string address) - // { - // websocketpp::lib::error_code ec; - // client::connection_ptr con = c.get_connection(address, ec); - // if (ec) { - // std::cout << "could not create connection because: " << ec.message() << std::endl; - // return; - // } - - // c.connect(con); - // } - - // void update() - // { - // c.run_one(); - // } - - - - - mg_mgr mgr; mg_connection *c = nullptr; - bool done = false; + bool isConnected = false; + + std::function onConnect; + std::function onClose; + std::function onError; static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) { - MessageBoxA(NULL, "", "Error", MB_OK); - } else if (ev == MG_EV_WS_OPEN) { - //mg_ws_send(c, "hello", 5, WEBSOCKET_OP_TEXT); + if (ev == MG_EV_WS_OPEN) { + puts("Open"); + isConnected = true; } else if (ev == MG_EV_WS_MSG) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; + printf("Msg: %.*s\n", (int)wm->data.len, wm->data.ptr); std::string jsonStr(wm->data.ptr, wm->data.len); auto msg = json::parse(jsonStr); int op = msg["op"].get(); @@ -93,22 +36,27 @@ namespace ws }} }; auto responseStr = response.dump(); - MessageBoxA(NULL, - (std::string("is_client") + std::to_string(c->is_client) + - std::string(" is_accepted") + std::to_string(c->is_accepted) + - std::string(" is_readable") + std::to_string(c->is_readable) + - std::string(" is_writable") + std::to_string(c->is_writable)).c_str(), - "", MB_OK); - mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT); + mg_ws_send(c, responseStr.c_str(), responseStr.size(), WEBSOCKET_OP_TEXT); } else if (op == 2) { - MessageBoxA(NULL, "hura", "connected", MB_OK); + puts("Connected"); + if (onConnect) + onConnect(); } } - if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) { - done = true; + if (ev == MG_EV_ERROR) { + printf("Error: %s\n", (char*)ev_data); + isConnected = false; + if (onError) + onError(); + } + if (ev == MG_EV_CLOSE) { + puts("Close"); + isConnected = false; + if (onClose) + onClose(); } } @@ -124,20 +72,19 @@ namespace ws void update() { - if (c && !done) + if (c) mg_mgr_poll(&mgr, 10); - else - MessageBoxA(NULL, "cant update", "Nio", MB_OK); } - void identify() + void sendRequest(std::string requestType) { - MessageBoxA(NULL, - (std::string("is_client") + std::to_string(c->is_client) + - std::string(" is_accepted") + std::to_string(c->is_accepted) + - std::string(" is_readable") + std::to_string(c->is_readable) + - std::string(" is_writable") + std::to_string(c->is_writable)).c_str(), - "", MB_OK); - mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT); + json request = { { "op", 6 }, + { "d", + { + { "requestType", requestType }, + { "requestId", std::to_string(time(nullptr)).c_str() } + } } }; + auto requestStr = request.dump(); + mg_ws_send(c, requestStr.c_str(), requestStr.size(), WEBSOCKET_OP_TEXT); } } \ No newline at end of file