FredyH / GWSockets

WebSockets for GLua
MIT License
87 stars 7 forks source link

timers don't seem to work when functions are called from a function like "socket:onMessage()" #37

Closed GoldenStarGamer closed 11 months ago

GoldenStarGamer commented 11 months ago

I have this program, i made it to try to connect to discord, lil project of mine:

require("gwsockets")
local discord = GWSockets.createWebSocket("wss://gateway.discord.gg/?v=10&encoding=json")

function discord:onMessage(txt)
    print(txt)
    process(util.JSONToTable(txt))
end

function heartbeat()
    discord:write('{"op":1}')
end

function StartHeartbeat(number)
    print("start the heartbeats")
    timer.Simple(number, heartbeat)
    timer.Start("heartbeat")
end

function process(data)
    if data and data.op == 10 then
        StartHeartbeat(data.d.heartbeat_interval)
    end
end

function discord:onError(txt)
    print("Error: ", txt)
end

function discord:onConnected()

    print("Connected to echo server")

end

function discord:onDisconnected()
    print("WebSocket disconnected")
    timer.Destroy("heartbeat")
end

concommand.Add("ds_openws", function() discord:open() end)

but, heartbeat does not run, StartHeartbeat() runs but heartbeat() doesn't.

FredyH commented 11 months ago

From what I can see, I don't think you ever start the timer using timer.Create.

GoldenStarGamer commented 11 months ago

From what I can see, I don't think you ever start the timer using timer.Create.

yes i changed it temporarily to a timer.simple but i tried with timer.create then timer.start and still nothing

FredyH commented 11 months ago

You are probably not using the timer function correctly, see here for how it is used: https://wiki.facepunch.com/gmod/timer.Create

GoldenStarGamer commented 11 months ago

You are probably not using the timer function correctly, see here for how it is used: https://wiki.facepunch.com/gmod/timer.Create

nope, I seem to have written it correctly, however im going to try again later

GoldenStarGamer commented 11 months ago

it's still an issue, it doesn't work, anyway im going to make my own bin module

FredyH commented 11 months ago

I don't know what to tell you, I ran your code (after correcting the timer usage) and it runs the heartbeat just fine. The problem is that discord terminates the connection if you write the payload you used, probably because you are not authorized.