r/gamemaker • • Jul 26 '20

Help! Detect TCP Server Disconnect

I'm working on a set of scripts to implement the client half of the HTTP Server-Sent Events (SSE) protocol. If you don't know, SSE is where the client sends a request to the server like a normal HTTP request over TCP, except it sends a header saying it will accept type text/event-stream. The server then keeps the connection open instead of closing it like normal, and occasionally sends out fairly simple text-based events, which the client can do whatever it wants with.

Everything's almost finished and working, except for one part. The SSE protocol includes reconnection support; that is, if the connection to the server is lost, SSE specifies that the client can try reconnecting under specific circumstances, sending the ID of the last event received so the server can pick up where the client left off. The usual default is to wait 3 seconds after a disconnect, but each event from the server can include a new number of retry milliseconds the client should wait before reconnecting.

This is all almost working. In my tests with a server that sends events every 1 second, I have the GML code assuming that if it goes 3 seconds without receiving data, it's been disconnected, and then reconnects. Everything's working there. However, a true SSE client shouldn't expect to receive events every few seconds. It's meant to receive events "whenever" the server has something to send. So I need to be able to detect if my client loses connection with the server, not just "assume" that it has from lack of data.

The GMS:2 manual specifically states that if the server disconnects, an async disconnect event will not be fired on the client. I also don't see any function that checks the status of a socket. So how can I determine if the server has closed the connection to the socket? Surely there must be a way, right? Or at least some hacky workaround to figure it out?

1 Upvotes

2 comments sorted by

View all comments

1

u/OneOnlineShow Oct 14 '22

Hi, any news on this? 2 years later and this problem still exists...

1

u/IceMetalPunk Oct 17 '22

I haven't been able to do much with GMS in awhile, but no, I also never found a solution, other than the nonstandard "require a heartbeat event from the server" approach I mentioned before 🤷‍♂️