Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Relay' started by Max_power1965, Sep 25, 2022.

  1. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    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
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    I was just about to ask the same question
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    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
     
    unity_zf-8YWQQUOW3Kg likes this.
  4. UhBigMoney

    UhBigMoney

    Joined:
    Nov 26, 2019
    Posts:
    1
    I have the same question. What is the proper way to send a close message? is there a function that can be called?
     
  5. AOSandberg

    AOSandberg

    Joined:
    Sep 9, 2020
    Posts:
    15
    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.
     
  6. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    I assume that just dissconecting regularly as the network manager should close the connection because relay handles the transport.
     
  7. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    156
    Hi,
    Since my question is about "CLOSE MESSAGE", I wanted to write the message here and did not want to open a new thread.

    Question:
    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.
     
  8. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    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
     
  9. BGagnon_Unity

    BGagnon_Unity

    Unity Technologies

    Joined:
    Mar 9, 2022
    Posts:
    5
    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.
     
    Mj-Kkaya likes this.
  10. mo1ok

    mo1ok

    Joined:
    Mar 26, 2016
    Posts:
    4
    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.