fix: websocket func

This commit is contained in:
Linventif 2024-07-09 08:18:29 +00:00
parent bbe66a70ae
commit 9324d7c293

View File

@ -20,19 +20,12 @@ local function getWebSocketURL()
return method .. "://" .. gmInte.config.websocketFQDN return method .. "://" .. gmInte.config.websocketFQDN
end end
function gmInte.setupWebSocket()
local socket = GWSockets.createWebSocket(getWebSocketURL()) local socket = GWSockets.createWebSocket(getWebSocketURL())
socket:setHeader("id", gmInte.config.id) socket:setHeader("id", gmInte.config.id)
socket:setHeader("token", gmInte.config.token) socket:setHeader("token", gmInte.config.token)
function gmInte.resetWebSocket() socket:open()
socket:closeNow()
socket = GWSockets.createWebSocket(getWebSocketURL())
socket:setHeader("id", gmInte.config.id)
socket:setHeader("token", gmInte.config.token)
end
local hasConnected = false
function socket:onConnected() function socket:onConnected()
hasConnected = true
gmInte.log("WebSocket Connected", true) gmInte.log("WebSocket Connected", true)
end end
@ -47,12 +40,7 @@ function socket:onMessage(txt)
end end
function socket:onDisconnected() function socket:onDisconnected()
if hasConnected then
hasConnected = false
gmInte.log("WebSocket Disconnected", true) gmInte.log("WebSocket Disconnected", true)
else
gmInte.logError("WebSocket Connection Failed", true)
end
end end
function socket:onError(txt) function socket:onError(txt)
@ -61,14 +49,11 @@ end
timer.Create("gmInte:WebSocket:CheckConnection", 4, 0, function() timer.Create("gmInte:WebSocket:CheckConnection", 4, 0, function()
if !socket:isConnected() then if !socket:isConnected() then
gmInte.resetWebSocket() gmInte.log("WebSocket is not connected, trying to reconnect", true)
socket:open() timer.Remove("gmInte:WebSocket:CheckConnection")
gmInte.setupWebSocket()
end end
end) end)
end
hook.Add("InitPostEntity", "gmInte:ServerReady:WebSocket", function() hook.Add("InitPostEntity", "gmInte:ServerReady:WebSocket", function() timer.Simple(1, function() gmInte.setupWebSocket() end) end)
timer.Simple(1, function()
gmInte.resetWebSocket()
socket:open()
end)
end)