Skip to content
This repository was archived by the owner on Sep 2, 2019. It is now read-only.

Commit 20694aa

Browse files
committed
Added web sockets settings and status display.
1 parent 94e93dc commit 20694aa

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/awio/overlay.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <boost/algorithm/string.hpp>
2323
#include <boost/algorithm/hex.hpp>
2424

25+
26+
#include <atlstr.h>
27+
#define STR2UTF8(s) (CW2A(CA2W(s), CP_UTF8))
28+
2529
// This source code seems to require refactoring. :<
2630
// Until then, focus on the features. I am not yet familiar with dear imgui.
2731

@@ -32,7 +36,10 @@ std::unordered_map<ImGuiContext*, bool> font_inited;
3236

3337
static bool show_name = true;
3438

39+
static char websocket_message[1024] = { 0, };
40+
static char websocket_host[256] = { 0, };
3541
static char websocket_port[256] = { 0, };
42+
static bool websocket_reconnect = true;
3643
static void* overlay_texture = nullptr;
3744
static unsigned char *overlay_texture_filedata = nullptr;
3845
static int overlay_texture_width = 0, overlay_texture_height = 0, overlay_texture_channels = 0;
@@ -260,19 +267,32 @@ class WebSocket
260267
boost::asio::ip::tcp::resolver r{ ios };
261268
try {
262269
// localhost only !
263-
std::string const host = "127.0.0.1";
264270
boost::asio::ip::tcp::socket sock{ ios };
271+
std::string host = websocket_host;
265272
auto endpoint = r.resolve(boost::asio::ip::tcp::resolver::query{ host, websocket_port });
266273
std::string s = endpoint->service_name();
267274
uint16_t p = endpoint->endpoint().port();
268275
boost::asio::connect(sock, endpoint);
269276

270277
beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{ sock };
278+
271279
std::string host_port = host + ":" + websocket_port;
272280
ws.handshake(host_port, "/MiniParse");
273281

282+
websocket_reconnect = false;
283+
284+
if (sock.is_open())
285+
{
286+
strcpy_s(websocket_message, 1023, "Connected");
287+
}
274288
while (sock.is_open() && loop)
275289
{
290+
if (websocket_reconnect)
291+
{
292+
sock.close();
293+
std::cout << "Closed" << "\n";
294+
break;
295+
}
276296
beast::multi_buffer b;
277297
beast::websocket::opcode op;
278298
ws.read(op, b);
@@ -393,15 +413,16 @@ class WebSocket
393413
}
394414
catch (std::exception& e)
395415
{
416+
strcpy_s(websocket_message, 1023, STR2UTF8(e.what()));
396417
std::cerr << e.what() << std::endl;
397418
}
398419
catch (...)
399420
{
400-
421+
strcpy_s(websocket_message, 1023, "Unknown exception");
401422
}
402423
if (!loop)
403424
break;
404-
for (int i = 0; i<50 && loop; ++i)
425+
for (int i = 0; i<50 && loop && !websocket_reconnect; ++i)
405426
Sleep(100);
406427
if (!loop)
407428
break;
@@ -742,6 +763,7 @@ extern "C" int ModInit(ImGuiContext* context)
742763
{
743764
show_name = setting.get("show_name", true).asBool();
744765
default_pet_job = setting.get("default_pet_job", default_pet_job).asString();
766+
strcpy(websocket_host, setting.get("websocket_host", "127.0.0.1").asCString());
745767
strcpy(websocket_port, setting.get("websocket_port", "10501").asCString());
746768
Json::Value color = setting.get("color_map", Json::Value());
747769
for (auto i = color.begin(); i != color.end(); ++i)
@@ -911,6 +933,7 @@ void SaveSettings()
911933
Json::StyledWriter w;
912934
Json::Value setting;
913935
setting["show_name"] = show_name;
936+
setting["websocket_host"] = websocket_host;
914937
setting["websocket_port"] = websocket_port;
915938
setting["default_pet_job"] = default_pet_job;
916939

@@ -1410,12 +1433,22 @@ void Preference(ImGuiContext* context, bool* show_preferences)
14101433
}
14111434
if (ImGui::TreeNode("WebSocket"))
14121435
{
1413-
if (ImGui::InputText("WebSocket Port", websocket_port, 50, ImGuiInputTextFlags_CharsDecimal))
1436+
if (ImGui::InputText("Host", websocket_host, 50))
1437+
{
1438+
websocket_reconnect = true;
1439+
strcpy_s(websocket_message, 1023, "Connecting...");
1440+
SaveSettings();
1441+
}
1442+
if (ImGui::InputText("Port", websocket_port, 50, ImGuiInputTextFlags_CharsDecimal))
14141443
{
1444+
websocket_reconnect = true;
1445+
strcpy_s(websocket_message, 1023, "Connecting...");
14151446
SaveSettings();
14161447
}
14171448
ImGui::TreePop();
14181449
}
1450+
ImGui::Text("WebSocket Status : %s", websocket_message);
1451+
ImGui::Separator();
14191452
if (ImGui::TreeNode("Opacity"))
14201453
{
14211454
static int group_opacity = 255;

0 commit comments

Comments
 (0)