From c72fdb5100510a03f5d5a52762c9adebdc4883f0 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 + Fix crash + isConnected() + hasListen() + resetReconnect() --- .../vBot_4.8/vBot/BotServer.lua | 1 + modules/game_bot/functions/server.lua | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) 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..774372b 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 @@ -60,6 +60,7 @@ if rootWidget then if BotServer._websocket then BotServer.terminate() end + BotServer.resetReconnect() botServerWindow.Data.ServerStatus:setText("DISCONNECTED") ui.botServer:setColor('#E3242B') botServerWindow.Data.ServerStatus:setColor('#E3242B') diff --git a/modules/game_bot/functions/server.lua b/modules/game_bot/functions/server.lua index f620fc5..519b339 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