Skip to content

Commit

Permalink
BotServer updates
Browse files Browse the repository at this point in the history
+ Fix crash
+ isConnected()
+ hasListen()
+ resetReconnect()
  • Loading branch information
Leesneaks committed Mar 10, 2024
1 parent 29307dc commit c72fdb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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 c72fdb5

Please sign in to comment.