I have a situation when a player will create a Lobby and consequentially bind to a Relay server as a host, now in case the user gets tired of waiting for other players and presses the close button, it should also be immediately unbound from the relay server.
How do I do that?
I’ve seen that there is an API to send a Close message but can someone show me and example?
I think that unity transport handles it for you so if you are using netcode for game objects doing a NetworkManager.Singleton.Shutdown() should close the connection to the relay server
I’m not sure if this is the correct way, but I found something that works. The thing is that if you want to disconnect a client or the client want to leave this must be done from server side. So what you can do is get the clientId from the client that want to disconnect, call a ServerRPC that calls a function on the host with the id and close it from there. Closing the connection can be done by calling NetworkManager.Singleton.DisconnectClient(clientId) and then call Destory on the playernetwork of the client like this Destory(clientPlayernetwork). Doing this will disconnect the client, and you can handle what should happen when the client is disconnected on the clientside by writing code within the OnNetworkDestory in the playernetwork of the client. Make sure to check if the destoyed network is the owner because you dont want the behavior of the disconnect to happen if another player is disconecting.
There is no API to send an explicit CLOSE message. Unity Transport Package, Netcode for Game Objects and Netcode for Entities handle Relay disconnection on their own.
The protocol documentation is meant as a low-level reference for someone implementing their own transport library (for Unity or other engine) to use Unity Relay. If not using a library and implementing the socket protocol directly, then sending a CLOSE message is a good practice, but not strictly required. The TTL expiration on each Relay allocation ensures that resources are cleaned up if the peer does not send any traffic within 10 seconds.
Note that in the case of TCP protocols like websocket (ws) and secure websocket (wss), there is another disconnection signal inherent to the connection. This isn’t the case with UDP and DTLS.
I have the same question. This documentation page desperately needs examples of how to send and receive events. In my case, I’m trying to figure out how to listen for disconnect events, and also send close events.