1

I have a socket server in nodejs, a unity client and I try calculate the latency between them like below Server:

socket.on("readable", () => {
    let data: Buffer = socket.read();
    if(data !== null) {
        let length: number = parseInt(bufferStream.subarray(0, maxDataLength).toString("hex"), 16);
        let dataBuffer : Buffer = bufferStream.subarray(maxDataLength, maxDataLength + length);
        let data: string = dataBuffer.toString('utf-8');
        let json: any = JSON.parse(data);
        switch (json._event.event_name) {
            case 'ping':
                let pingData = {
                    event_name : "pong"
                }
                socket.write((PrepareData(pingData));
                break;
        }
    }
});

Client:

class PingData
{
    public static float start = 0;
    public static bool pinged = false;
}

private async void SocketReading()
{
    if (socket.Available == 0)
    {
        return;
    }
    byte[] buf = new byte[socket.Available];
    await socket.ReceiveAsync(buf, SocketFlags.None);
    buffer.AddRange(buf);
}

private void ProcessBuffer()
{
    byte[] lengthField = buffer.Take(4).ToArray();
    int dataLength = BitConverter.ToInt32(lengthField, 0);

    byte[] dataField = buffer.Skip(4).Take(dataLength).ToArray();
    string response = Encoding.UTF8.GetString(dataField);

     EventName _event = JsonUtility.FromJson<EventName>(response);

    switch (_event.event_name)
    {
        case "pong":
        Debug.Log((Time.time - PingData.start) * 1000f);
        PingData.pinged = false;
        break;
        //other cases
    }
}

public void Ping()
{
    SendData<PingEvent> data = new SendData<PingEvent>(new PingEvent());
    Send(JsonUtility.ToJson(data));
    PingData.start = Time.time;
    PingData.pinged  = true;
}

Is this the correct way to get the ping between server and client? Because the log give very high number (20-100) even when test in same computer.

6
  • Sidenote: did you know that there is an entire exchange specifically for these kind of questions? It's located here: gamedev.stackexchange.com . You'll find more related questions and a sometimes more likely answers over there. Commented Jul 12, 2024 at 6:07
  • if you only want to find the latency to the server you could use the dotnet ping class Commented Jul 12, 2024 at 6:10
  • 1
    The persistent myth that the round-trip time reported by ping is directly related to game performance. Ping uses ICMP, but games typically use UDP (never ICMP), and the performance between the two protocols can vary wildly. Ping is a tool to check for IP connectivity, and it is not a good tool to check for latency because ICMP is the least concern for routers to forward, and it is often delayed. Commented Jul 12, 2024 at 13:13
  • If your send function is async or batches requests, then that'll mess with your ping times. Commented Jul 17, 2024 at 16:14
  • @ipodtouch0218 yes, my send function is async. Should i create a thread to handle the send action? Commented Jul 19, 2024 at 8:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.