From 53d0c2400b426a33115f119b8554607665df6fbf Mon Sep 17 00:00:00 2001 From: Jon <> Date: Mon, 12 Aug 2024 12:00:11 +0100 Subject: [PATCH 1/2] Moved sender websocket to global scope, change websocket functions to use pointers --- GuildWarsPartySearch.Bot/main.cpp | 93 ++++++++++++++++--------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/GuildWarsPartySearch.Bot/main.cpp b/GuildWarsPartySearch.Bot/main.cpp index eae9325..883e19e 100644 --- a/GuildWarsPartySearch.Bot/main.cpp +++ b/GuildWarsPartySearch.Bot/main.cpp @@ -121,14 +121,12 @@ static CallbackEntry EventType_PartySearchType_entry; static CallbackEntry EventType_WorldMapEnter_entry; static CallbackEntry EventType_WorldMapLeave_entry; -static easywsclient::WebSocket::pointer ws; - static bool party_advertisements_pending = true; static std::vector party_search_advertisements; -static easywsclient::WebSocket::pointer connect_websocket(); -static void disconnect_websocket(); +static bool connect_websocket(easywsclient::WebSocket::pointer* websocket_pt, const std::string& url); +static bool disconnect_websocket(easywsclient::WebSocket::pointer* websocket_pt); static std::string get_next_argument(int current_index) { if (current_index <= GetArgc()) { @@ -224,25 +222,27 @@ static bool is_websocket_ready(easywsclient::WebSocket::pointer ws) { return ws && ws->getReadyState() == easywsclient::WebSocket::OPEN; } -static int send_websocket(const std::string& payload) { - if(!is_websocket_ready(ws)) - connect_websocket(); +static bool send_websocket(easywsclient::WebSocket::pointer websocket, const std::string& payload) { + if (!is_websocket_ready(websocket)) { + return false; + } LogInfo("Websocket send:"); printf("%s\n", payload.c_str()); last_websocket_message = time_get_ms(); - ws->send(payload); - return 0; + websocket->send(payload); + return true; } -static void send_ping() { - if (!is_websocket_ready(ws)) +static void send_ping(easywsclient::WebSocket::pointer websocket) { + if (!is_websocket_ready(websocket)) return; // No need to connect if its not ready LogInfo("Sending ping"); last_websocket_message = time_get_ms(); - ws->sendPing(); + websocket->sendPing(); } -static int send_party_advertisements() { +static int send_party_advertisements(easywsclient::WebSocket::pointer websocket) { + assert(is_websocket_ready(websocket)); std::vector ads; for (const auto& ad : party_search_advertisements) { if (!ad) { @@ -258,7 +258,7 @@ static int send_party_advertisements() { j["parties"] = ads; const auto payload = j.dump(); - return send_websocket(payload); + return send_websocket(websocket, payload); } static void collect_instance_info() { @@ -440,22 +440,28 @@ static void on_websocket_message(const std::string& message) { last_websocket_message = time_get_ms(); } -static void disconnect_websocket() { - if (!ws) return; - ws->close(); +static bool disconnect_websocket(easywsclient::WebSocket::pointer* websocket_pt) { + if (!websocket_pt) + return false; + auto websocket = *websocket_pt; + if (!websocket) + return true; + + websocket->close(); // Wait for websocket to close for (auto j = 0; j < 5000; j+=50) { - ws->poll(); - if (ws->getReadyState() == easywsclient::WebSocket::CLOSED) + websocket->poll(); + if (websocket->getReadyState() == easywsclient::WebSocket::CLOSED) break; time_sleep_ms(50); } - ws = NULL; + *websocket_pt = NULL; + return true; } -static easywsclient::WebSocket::pointer connect_websocket() { - if (is_websocket_ready(ws)) - return ws; - assert(*account_uuid && map_id); +static bool connect_websocket(easywsclient::WebSocket::pointer* websocket_pt, const std::string& url) { + assert(websocket_pt && url.size() && *account_uuid && map_id); + if (is_websocket_ready(*websocket_pt)) + return true; char user_agent[255]; char uuid_less_hyphens[ARRAY_SIZE(account_uuid)] = { 0 }; size_t added = 0; @@ -468,26 +474,23 @@ static easywsclient::WebSocket::pointer connect_websocket() { const auto connect_retries = 10; for (auto i = 0; i < connect_retries; i++) { - disconnect_websocket(); - LogInfo("Attempting to connect. Try %d/%d", i + 1, connect_retries); - ws = easywsclient::WebSocket::from_url(bot_configuration.web_socket_url, user_agent); - if (!ws) { - // Sleep before retry - time_sleep_sec(5); - continue; - } + auto websocket = easywsclient::WebSocket::from_url(url, user_agent); // Wait for websocket to open - for (auto j = 0; j < 5000; j+=50) { - ws->poll(); - if (is_websocket_ready(ws)) { + for (auto j = 0; websocket && j < 5000; j+=50) { + websocket->poll(); + if (is_websocket_ready(websocket)) { + *websocket_pt = websocket; LogInfo("Websocket opened successfully"); party_advertisements_pending = true; last_websocket_message = time_get_ms(); - return ws; + return websocket; } time_sleep_ms(50); } + disconnect_websocket(&websocket); + // Sleep before retry + time_sleep_sec(5); } exit_with_status("Failed to connect to websocket", FAILED_TO_CONNECT); @@ -517,26 +520,28 @@ static int main_bot(void* param) wait_until_ingame(); + easywsclient::WebSocket::pointer sending_websocket; + while (running) { wait_until_ingame(); ensure_correct_outpost(); - connect_websocket(); + assert(connect_websocket(&sending_websocket, bot_configuration.web_socket_url)); if (party_advertisements_pending) { - send_party_advertisements(); + send_party_advertisements(sending_websocket); party_advertisements_pending = false; } - if (ws) { + if (sending_websocket) { if (time_get_ms() - last_websocket_message > 30000) { - send_ping(); + send_ping(sending_websocket); } - ws->dispatch(on_websocket_message); - ws->poll(); + sending_websocket->dispatch(on_websocket_message); + sending_websocket->poll(); } time_sleep_ms(50); } + disconnect_websocket(&sending_websocket); -cleanup: UnRegisterEvent(&EventType_PartySearchAdvertisement_entry); UnRegisterEvent(&EventType_PartySearchRemoved_entry); UnRegisterEvent(&EventType_PartySearchSize_entry); @@ -545,7 +550,7 @@ static int main_bot(void* param) UnRegisterEvent(&EventType_WorldMapLeave_entry); clear_party_search_advertisements(); - disconnect_websocket(); + raise(SIGTERM); return 0; } From 8bc2286e2b75bffc72f07c1a27f2af63d5973884 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Mon, 12 Aug 2024 13:04:50 +0200 Subject: [PATCH 2/2] add api key to config * Update config.example.sh (add api key) * Update run.sh --- GuildWarsPartySearch.Bot/config.example.sh | 8 +++++--- GuildWarsPartySearch.Bot/run.sh | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/GuildWarsPartySearch.Bot/config.example.sh b/GuildWarsPartySearch.Bot/config.example.sh index ef0793c..7208982 100644 --- a/GuildWarsPartySearch.Bot/config.example.sh +++ b/GuildWarsPartySearch.Bot/config.example.sh @@ -10,9 +10,11 @@ EMAIL="" PASSWORD="" # Character name to travel to listen for party searches CHARACTER="" -# Map ID to travel to listen for party searches -MAP_ID="148" +# Map ID to travel to listen for party searches, default embark beach +MAP_ID="857" # Composite district enum to use (e.g. 1 = AMERICAN) - check source code! DISTRICT="1" +# API Key, necessary +WEBSOCKET_API_KEY="development" # Endpoint to send party searches to -WEBSOCKET_URL="ws://hostname/party-search/update" \ No newline at end of file +WEBSOCKET_URL="ws://localhost/party-search/update" diff --git a/GuildWarsPartySearch.Bot/run.sh b/GuildWarsPartySearch.Bot/run.sh index fd1cba1..cccdfae 100644 --- a/GuildWarsPartySearch.Bot/run.sh +++ b/GuildWarsPartySearch.Bot/run.sh @@ -36,6 +36,7 @@ RUN_CMD=$(cat <