X-Git-Url: https://gitweb.ps.run/autorec/blobdiff_plain/e90fc8a9809127820252a09dd7bfbbbc7a6f295d..8d3cfb2ed7f4d2a70832526e1a2a68926e8a8d4b:/src/ws.h diff --git a/src/ws.h b/src/ws.h index ecc88b3..4bebc1f 100644 --- a/src/ws.h +++ b/src/ws.h @@ -4,6 +4,7 @@ using json = nlohmann::json; #include +#include namespace ws { @@ -12,10 +13,14 @@ namespace ws bool done = false; + std::function onConnect; + static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_WS_OPEN) { + puts("Open"); } 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(); @@ -32,16 +37,18 @@ namespace ws } else if (op == 2) { - MessageBoxA(NULL, "hura", "connected", MB_OK); + puts("Connected"); + if (onConnect) + onConnect(); } } if (ev == MG_EV_ERROR) { - MessageBoxA(NULL, "", "Error", MB_OK); + printf("Error: %s\n", (char*)ev_data); done = true; } if (ev == MG_EV_CLOSE) { - MessageBoxA(NULL, "", "Close", MB_OK); + puts("Close"); done = true; } } @@ -68,7 +75,7 @@ namespace ws { "d", { { "requestType", requestType }, - { "requestId", std::to_string(NULL).c_str() } + { "requestId", std::to_string(time(nullptr)).c_str() } } } }; auto requestStr = request.dump(); mg_ws_send(c, requestStr.c_str(), requestStr.size(), WEBSOCKET_OP_TEXT);