diff options
| author | Patrick Schönberger | 2022-01-09 21:31:23 +0100 |
|---|---|---|
| committer | Patrick Schönberger | 2022-01-09 21:31:23 +0100 |
| commit | e90fc8a9809127820252a09dd7bfbbbc7a6f295d (patch) | |
| tree | 8927270177c1f0b467f31d5039d08ccc07958a6e /src/ws.h | |
| parent | 8d2244aefe172b5b1e15a32ad23991945d128f3a (diff) | |
| download | autorec-e90fc8a9809127820252a09dd7bfbbbc7a6f295d.tar.gz autorec-e90fc8a9809127820252a09dd7bfbbbc7a6f295d.zip | |
MVP
Diffstat (limited to 'src/ws.h')
| -rw-r--r-- | src/ws.h | 101 |
1 files changed, 17 insertions, 84 deletions
@@ -1,10 +1,3 @@ -// #define _WEBSOCKETPP_CPP11_INTERNAL_
-// #define ASIO_STANDALONE
-// #include <websocketpp/config/asio_no_tls_client.hpp>
-// #include <websocketpp/client.hpp>
-
-// using client = websocketpp::client<websocketpp::config::asio_client>;
-
#include "mongoose.h"
#include "json.hpp"
@@ -14,71 +7,13 @@ using json = nlohmann::json; 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<int>();
-
- // 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;
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) {
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
std::string jsonStr(wm->data.ptr, wm->data.len);
@@ -93,13 +28,7 @@ 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)
{
@@ -107,7 +36,12 @@ namespace ws }
}
- if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
+ if (ev == MG_EV_ERROR) {
+ MessageBoxA(NULL, "", "Error", MB_OK);
+ done = true;
+ }
+ if (ev == MG_EV_CLOSE) {
+ MessageBoxA(NULL, "", "Close", MB_OK);
done = true;
}
}
@@ -126,18 +60,17 @@ namespace ws {
if (c && !done)
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(NULL).c_str() }
+ } } };
+ auto requestStr = request.dump();
+ mg_ws_send(c, requestStr.c_str(), requestStr.size(), WEBSOCKET_OP_TEXT);
}
}
\ No newline at end of file |
