Remove Player From Relay Server (how to send CLOSE message)

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?

Thanks

I was just about to ask the same question

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

1 Like

I have the same question. What is the proper way to send a close message? is there a function that can be called?

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.

Code snippets I use:

'**************************************************************************************
PLAYERNETWORK:

This calls disconnect on the serverside in the Gamemanager (a manager i made myself but can be anywhere)

[ServerRpc]
public void DisconnectSelfServerRPC(ulong playerId)
{
GameManager.instance.DisconenctPlayer(playerId);
}


This function is called when the client disconnects

public override void OnNetworkDespawn()
{
if (IsOwner)
NavigationManager.Instance.GoToScene(1);
}

'*****************************************************************************************************

GAMEMANAGER (a class i made myself)

Is called by the serverRPC and disconnects the plaeyr with given id and then destroys the playergameobject

public void DisconenctPlayer(ulong clientId)
{
NetworkManager.Singleton.DisconnectClient(clientId);
Destroy(clientNetwork);

}

Hope this helps somebody.

I assume that just dissconecting regularly as the network manager should close the connection because relay handles the transport.

Hi,
Since my question is about “CLOSE MESSAGE”, I wanted to write the message here and did not want to open a new thread.
( Remove Player From Relay Server (how to send CLOSE message) members/max_power1965.452010/)
Question:( Remove Player From Relay Server (how to send CLOSE message) members/max_power1965.452010/)
How can we use Relay message protocol.
protocols: https://docs.unity.com/relay/en/manual/relay-message-protocol

We can use NetworkManager but I want to learn use without NM.

I think but not sure it is related to the transport. Maybe you can look how the NM is doing it and do something simmilar

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.

https://docs.unity.com/relay/en/manual/client-timeouts
https://docs.unity.com/relay/en/manual/keep-connection-alive

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.

1 Like

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.