Skip to content

Commit

Permalink
BotServer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Leesneaks authored and Luan Luciano committed Mar 11, 2024
1 parent d923c48 commit a8127b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
13 changes: 7 additions & 6 deletions modules/game_bot/default_configs/vBot_4.8/vBot/BotServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down
39 changes: 38 additions & 1 deletion modules/game_bot/functions/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

0 comments on commit a8127b8

Please sign in to comment.