Advertisement

Websocket: ping and pong to calculate latency. Anything special?

Started by January 02, 2017 05:13 AM
0 comments, last by Rich76 7 years, 10 months ago

I want to calculate and display network latency on my game. Is there anything special about a ping request or a pong response, or is it the same as sending any data? I want the client to send pings to the server multiple times a second, and I want to make sure I'm doing it efficiently with little impact on the server (and the client). For example, do I just send the server a string "ping" and have the server respond back with a string "pong?"

Thank you

I was able to find this:

Pings and Pongs: The Heartbeat of WebSockets

At any point after the handshake, either the client or the server can choose to send a ping to the other party. When the ping is received, the recipient must send back a pong as soon as possible. You can use this to make sure that the client is still connected, for example.

A ping or pong is just a regular frame, but it's a control frame. Pings have an opcode of 0x9, and pongs have an opcode of 0xA. When you get a ping, send back a pong with the exact same Payload Data as the ping (for pings and pongs, the max payload length is 125). You might also get a pong without ever sending a ping; ignore this if it happens.

http://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers

This topic is closed to new replies.

Advertisement