From 8972561f5f702be32e99f9dd0c54f493bc18fa1c Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sat, 27 May 2023 09:35:40 -0500 Subject: [PATCH] Improvements to server list updater, iirc. --- lib/api/server_list_updater.rb | 49 +++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/api/server_list_updater.rb b/lib/api/server_list_updater.rb index 3e78d87..48bba2b 100644 --- a/lib/api/server_list_updater.rb +++ b/lib/api/server_list_updater.rb @@ -11,7 +11,11 @@ class W3DHub @@instance = ServerListUpdater.new end + attr_accessor :auto_reconnect + def initialize + @auto_reconnect = false + logger.info(LOG_TAG) { "Starting emulated SignalR Server List Updater..." } run end @@ -20,6 +24,11 @@ class W3DHub Thread.new do begin connect + + while W3DHub::BackgroundWorker.alive? + connect if @auto_reconnect + sleep 1 + end rescue => e puts e puts e.backtrace @@ -34,7 +43,7 @@ class W3DHub end def connect - auto_reconnect = false + @auto_reconnect = false logger.debug(LOG_TAG) { "Requesting connection token..." } response = Excon.post("https://gsh.w3dhub.com/listings/push/v2/negotiate?negotiateVersion=1", headers: Api::DEFAULT_HEADERS, body: "") @@ -44,7 +53,21 @@ class W3DHub endpoint = "https://gsh.w3dhub.com/listings/push/v2?id=#{id}" logger.debug(LOG_TAG) { "Connecting to websocket..." } + this = self WebSocket::Client::Simple.connect(endpoint, headers: Api::DEFAULT_HEADERS) do |ws| + ws.on(:open) do + logger.debug(LOG_TAG) { "Requesting json protocol, v1..." } + ws.send({ protocol: "json", version: 1 }.to_json + "\x1e") + + logger.debug(LOG_TAG) { "Subscribing to server changes..." } + Store.server_list.each_with_index do |server, i| + i += 1 + mode = 1 # 2 full details, 1 basic details + out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, mode] } + ws.send(out.to_json + "\x1e") + end + end + ws.on(:message) do |msg| msg = msg.data.split("\x1e").first @@ -60,37 +83,25 @@ class W3DHub id, data = hash[:arguments] server = Store.server_list.find { |s| s.id == id } server_updated = server&.update(data) - States::Interface.instance&.update_server_browser(server) if server_updated + + BackgroundWorker.foreground_job(-> {}, ->(result){ States::Interface.instance&.update_server_browser(server) }) if server_updated end end end end - ws.on(:open) do - logger.debug(LOG_TAG) { "Requesting json protocol, v1..." } - ws.send({ protocol: "json", version: 1 }.to_json + "\x1e") - - logger.debug(LOG_TAG) { "Subscribing to server changes..." } - Store.server_list.each_with_index do |server, i| - i += 1 - mode = 1 # 2 full details, 1 basic details - out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, mode] } - ws.send(out.to_json + "\x1e") - end - end - ws.on(:close) do |e| p e - auto_reconnect = true + this.auto_reconnect = true + ws.close end ws.on(:error) do |e| p e - auto_reconnect = true + this.auto_reconnect = true + ws.close end end - - connect if auto_reconnect end end end