From a8127b8974b7c36e94397f0bc8eb4f889d72dddd Mon Sep 17 00:00:00 2001 From: Lee <42119604+Leesneaks@users.noreply.github.com> Date: Sun, 10 Mar 2024 12:33:20 +0000 Subject: [PATCH] BotServer updates --- .../vBot_4.8/vBot/BotServer.lua | 13 ++++--- modules/game_bot/functions/server.lua | 39 ++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/modules/game_bot/default_configs/vBot_4.8/vBot/BotServer.lua b/modules/game_bot/default_configs/vBot_4.8/vBot/BotServer.lua index e6feb43..6d5e369 100644 --- a/modules/game_bot/default_configs/vBot_4.8/vBot/BotServer.lua +++ b/modules/game_bot/default_configs/vBot_4.8/vBot/BotServer.lua @@ -56,15 +56,16 @@ if rootWidget then botServerWindow.Data.ServerStatus:setText("CONNECTING...") ui.botServer:setColor('#FFF380') botServerWindow.Data.ServerStatus:setColor('#FFF380') - else + else if BotServer._websocket then BotServer.terminate() end + BotServer.resetReconnect() botServerWindow.Data.ServerStatus:setText("DISCONNECTED") ui.botServer:setColor('#E3242B') botServerWindow.Data.ServerStatus:setColor('#E3242B') botServerWindow.Data.Participants:setText("-") - botServerWindow.Data.Members:setTooltip('') + botServerWindow.Data.Members:setTooltip('') ServerMembers = {} serverCount = {} end @@ -122,7 +123,7 @@ function initBotServerListenFunctions() -- list BotServer.listen("list", function(name, data) - serverCount = regexMatch(json.encode(data), regex) + serverCount = regexMatch(json.encode(data), regex) ServerMembers = json.encode(data) end) @@ -159,12 +160,12 @@ function initBotServerListenFunctions() if config.broadcasts then broadcastMessage(name..": "..message) end - end) + end) end initBotServerListenFunctions() function updateStatusText() - if BotServer._websocket then + if BotServer._websocket then botServerWindow.Data.ServerStatus:setText("CONNECTED") botServerWindow.Data.ServerStatus:setColor('#03AC13') ui.botServer:setColor('#03AC13') @@ -182,7 +183,7 @@ function updateStatusText() text = text .. "\n" .. re[i][2] end end - botServerWindow.Data.Members:setTooltip(text) + botServerWindow.Data.Members:setTooltip(text) end end else diff --git a/modules/game_bot/functions/server.lua b/modules/game_bot/functions/server.lua index 251edc8..597824a 100644 --- a/modules/game_bot/functions/server.lua +++ b/modules/game_bot/functions/server.lua @@ -8,6 +8,24 @@ context.BotServer._callbacks = {} context.BotServer._lastMessageId = 0 context.BotServer._wasConnected = true -- show first warning +context.BotServer.stopReconnect = false +context.BotServer.reconnectAttempts = 0 +context.BotServer.maxReconnectAttempts = 5 +context.BotServer.reconnectDelay = 2000 + +local function tryReconnect(name, channel) + if not context.BotServer.stopReconnect and context.BotServer.reconnectAttempts < context.BotServer.maxReconnectAttempts then + context.BotServer.reconnectAttempts = context.BotServer.reconnectAttempts + 1 + local delay = context.BotServer.reconnectDelay * (2 ^ (context.BotServer.reconnectAttempts - 1)) + scheduleEvent(function() + context.BotServer.init(name, channel) + end, delay) + else + context.BotServer.stopReconnect = false + context.BotServer.reconnectAttempts = 0 + end +end + context.BotServer.init = function(name, channel) if not channel or not name or channel:len() < 1 or name:len() < 1 then return context.error("Invalid params for BotServer.init") @@ -16,6 +34,11 @@ context.BotServer.init = function(name, channel) return context.error("BotServer is already initialized") end context.BotServer._websocket = HTTP.WebSocketJSON(context.BotServer.url, { + onOpen = function() + context.BotServer._wasConnected = true + context.BotServer.reconnectAttempts = 0 + context.warn("BotServer connected.") + end, onMessage = function(message, socketId) if not context._websockets[socketId] then return g_http.cancel(socketId) @@ -55,11 +78,12 @@ context.BotServer.init = function(name, channel) end if context.BotServer._wasConnected then context.warn("BotServer disconnected") + HTTP.cancel(socketId) end context.BotServer._wasConnected = false context.BotServer._websocket = nil context.BotServer.ping = 0 - context.BotServer.init(name, channel) + tryReconnect(name, channel) end }, context.BotServer.timeout) context._websockets[context.BotServer._websocket.id] = 1 @@ -70,6 +94,7 @@ context.BotServer.terminate = function() if context.BotServer._websocket then context.BotServer._websocket:close() context.BotServer._websocket = nil + context.BotServer._callbacks = {} end end @@ -89,3 +114,15 @@ context.BotServer.send = function(topic, message) end context.BotServer._websocket.send({type="message", topic=topic, message=message}) end + +context.BotServer.isConnected = function() + return context.BotServer._wasConnected and context.BotServer._websocket ~= nil +end + +context.BotServer.hasListen = function(topic) + return context.BotServer._callbacks and context.BotServer._callbacks[topic] ~= nil +end + +context.BotServer.resetReconnect = function() + context.BotServer.stopReconnect = true +end