Search Unity

Resolved How can I get the PING (Latency) of a match before joining it as a Client?

Discussion in 'Relay' started by rodrigo_unity487, Mar 14, 2023.

  1. rodrigo_unity487

    rodrigo_unity487

    Joined:
    Feb 23, 2022
    Posts:
    8
    Context: My multiplayer game uses Unity Relay to guarantee that players will be able to connect, and it also uses Unity Lobby to publicly display matches and their data. I'm currently trying to include game latency in the Lobby data, so as to be able to order the matches by lowest latency.

    Problem: I've not been able to find a simple way to get the latency of a match. My plan was to ping the selected relay server as a host to get the "Host to Relay" latency, and then do the same thing as a Client to get the "Client to Relay" latency, and finally add them up to get the estimated total latency the Client would have. However, I had problems trying to achieve this. Here's what I've tried so far:
    • Pinging The Relay IP: From the host side, I was able to get the Relay endpoint IP, so I tried using the simple Ping object to send a ping message. Unfortunately, the packet never arrived back, I assume the Relay servers drop normal ping messages :(
    • Sending a PING message via the Unity Services Web APIs: I tried manually using the web API to send ping messages, I tried using the Relay message protocol or the Qos service for this... However, I kept getting "unauthorized" errors. I guess I'm doing something wrong here, It seems that I require a special key that it's only accessible to the Organization owner for sending messages. Is that how it works? Or am I doing something wrong? My head hurts at this point... I also tried to get my credentials from my session in Unity Serives (since my game is done in Unity and I use the plugins and all), but I couldn't access any tokens, I've only got my allocation secret key but I honestly don't know how to use it, and I keep having trouble with the custom headers I need.
    Question:
    1. Is there any easy solution for my problem that I'm not seeing? This is taking me alot of time, and I might be missing something very simple and obvious. If I am, please let me know.
    2. Should I keep going with the Web API route? Does anyone have any idea what I might be doing wrong here?
    Thanks in advance...
     
  2. UnityRalahHaddad

    UnityRalahHaddad

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    10
    Hi rodrigo_unity487
    Regarding your question, we suggest using our QoS package as follows:
    • Depending on the region that the host will connect to, use the QoS package to get the host latency from the host to the QoS region
      Code (CSharp):
      1. qosResultsForRegion = await QosService.Instance.GetSortedQosResultsAsync("relay", qosRegion.Value);
    • Add to the lobby data the region and the host latency
    • From the client retrieve the region from the lobby data and use it to get the client latency to the host region
    • Retrieve the host latency from the Lobby data
    • By adding the host latency and the client latency you should be able to get a good estimation on the latency of the connection between the host and the client through the relay in that region
    I hope this helps,
    R
     
    Mj-Kkaya and rodrigo_unity487 like this.
  3. rodrigo_unity487

    rodrigo_unity487

    Joined:
    Feb 23, 2022
    Posts:
    8
    This worked perfectly! Thank you very much!