1 // #define _WEBSOCKETPP_CPP11_INTERNAL_
\r
2 // #define ASIO_STANDALONE
\r
3 // #include <websocketpp/config/asio_no_tls_client.hpp>
\r
4 // #include <websocketpp/client.hpp>
\r
6 // using client = websocketpp::client<websocketpp::config::asio_client>;
\r
8 #include "mongoose.h"
\r
11 using json = nlohmann::json;
\r
19 // void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
\r
20 // auto j = json::parse(msg->get_payload());
\r
21 // int op = j["op"].get<int>();
\r
24 // json response = { { "op", 1 },
\r
26 // { "rpcVersion", 1 },
\r
28 // auto responseStr = response.dump();
\r
29 // websocketpp::lib::error_code ec;
\r
30 // c.send(hdl, responseStr, websocketpp::frame::opcode::TEXT, ec);
\r
32 // MessageBoxA(NULL, ec.message().c_str(), "error", MB_OK);
\r
33 // } else if (op == 2) {
\r
34 // MessageBoxA(NULL, "hura", "connected", MB_OK);
\r
40 // c.set_access_channels(websocketpp::log::alevel::all);
\r
41 // c.clear_access_channels(websocketpp::log::alevel::frame_payload);
\r
42 // c.set_error_channels(websocketpp::log::elevel::all);
\r
44 // // Initialize ASIO
\r
47 // // Register our message handler
\r
48 // c.set_message_handler(&on_message);
\r
51 // void connect(std::string address)
\r
53 // websocketpp::lib::error_code ec;
\r
54 // client::connection_ptr con = c.get_connection(address, ec);
\r
56 // std::cout << "could not create connection because: " << ec.message() << std::endl;
\r
73 mg_connection *c = nullptr;
\r
77 static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
\r
78 if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
\r
79 MessageBoxA(NULL, "", "Error", MB_OK);
\r
80 } else if (ev == MG_EV_WS_OPEN) {
\r
81 //mg_ws_send(c, "hello", 5, WEBSOCKET_OP_TEXT);
\r
82 } else if (ev == MG_EV_WS_MSG) {
\r
83 struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
\r
84 std::string jsonStr(wm->data.ptr, wm->data.len);
\r
85 auto msg = json::parse(jsonStr);
\r
86 int op = msg["op"].get<int>();
\r
95 auto responseStr = response.dump();
\r
97 (std::string("is_client") + std::to_string(c->is_client) +
\r
98 std::string(" is_accepted") + std::to_string(c->is_accepted) +
\r
99 std::string(" is_readable") + std::to_string(c->is_readable) +
\r
100 std::string(" is_writable") + std::to_string(c->is_writable)).c_str(),
\r
102 mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT);
\r
106 MessageBoxA(NULL, "hura", "connected", MB_OK);
\r
110 if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
\r
120 void connect(std::string address)
\r
122 c = mg_ws_connect(&mgr, address.c_str(), cb, nullptr, nullptr);
\r
128 mg_mgr_poll(&mgr, 10);
\r
130 MessageBoxA(NULL, "cant update", "Nio", MB_OK);
\r
136 (std::string("is_client") + std::to_string(c->is_client) +
\r
137 std::string(" is_accepted") + std::to_string(c->is_accepted) +
\r
138 std::string(" is_readable") + std::to_string(c->is_readable) +
\r
139 std::string(" is_writable") + std::to_string(c->is_writable)).c_str(),
\r
141 mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT);
\r