Disconnect on close?
Asked Answered
T

8

0

Hey all. Working with some simple networking code and can't find how to disconnect the player when I close the client side program. When I run the server side and client side, if I stop or close the client program it never sends the disconnect signal to the server. So any code on that player still runs, but can't send it anywhere. On the server side I need to remove the player when they disconnect.

How do I run the disconnect code on the client side when I close the program?

How can I check to make sure a player is connected before trying to do anything with it?

Edit: I found the answer! Turns out I hadn't linked the "peer_disconnected" signal to anything. It was just sitting there doing nothing so once I hooked it up, I can work with a disconnected player!

Toniatonic answered 10/11, 2023 at 17:58 Comment(0)
A
0

I think you'll want to handle the quit request yourself and make sure you perform the disconnect there before actually quitting. https://docs.godotengine.org/en/stable/tutorials/inputs/handling_quit_requests.html

You can also have your server "heartbeat" the clients to make sure they are still connected and disconnect any that have timed out.

Alainaalaine answered 10/11, 2023 at 21:23 Comment(0)
T
0

Alainaalaine yeah I saw that in the docs, but was hoping there was a way to run a func whenever you close down the program. I guess I'll have to go with the heartbeat method then.

Is there any documentation on how to run a heartbeat in Godot? I'm not finding any info on it, but also not sure if I'm searching for the wrong thing.

Toniatonic answered 11/11, 2023 at 1:2 Comment(0)
C
0

Just keep the server side running persistently, and send a custom heartbeat req/ack packet every few second to test if connected clients are still responding.

Capacitate answered 11/11, 2023 at 4:7 Comment(0)
H
0

Toniatonic but was hoping there was a way to run a func whenever you close down the program.

What if the program closes or the connection is terminated in an emergency? Heartbeat is more reliable.

Hepatic answered 11/11, 2023 at 13:2 Comment(0)
T
0

Capacitate that sounds pretty simple. Is there a built in code for that or do I just build it myself with an rpc call?

Toniatonic answered 11/11, 2023 at 20:9 Comment(0)
T
0

Hepatic yeah I think I need to build that in. Not finding any info on a built in heartbeat system though which is unfortunate

Toniatonic answered 11/11, 2023 at 20:9 Comment(0)
T
0

Capacitate I found the answer! And a little embarrassed lol. I had the "peer_disconnected" code in there, but I never actually connected the signal for it to anything. So the player was disconnecting, and firing the signal for that disconnect, but I never did anything to link that signal to an actual process. Seems to be working fine now!

Toniatonic answered 11/11, 2023 at 20:34 Comment(0)
C
1

Toniatonic That only works when a peer is disconnected gracefully I think, you still need a check to test against erratic disconnection such as network failure or client power down etc.
Though I am not 100% sure about this on_disconnected callback's implementation taking edge cases into consideration.

Capacitate answered 12/11, 2023 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.