Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Websockets have implemented a keep alive mechanism using ping pong.
- going by https://www.rfc-editor.org/rfc/rfc6455.html
- section 5.5.2 and 5.5.3 tell us about what ping and pong are.
- Now we are supposed to expect a pong message with same data which was sent in the ping message.
- In websockets library in python , they follow the same.
- The steps that are taken are(avoiding ping_interval and ping_timeouts):
- * Generate a unique data (bytes) , map it to a dictionary and assign a asyncio future to it(consider it as a task).
- * Send the Ping with that uniquely generated data.
- * Now we wait for the client to respond back with a pong message with the "same data" , and if we receive it within 20 seconds(default keepalive timeout) we keep the connection alive , else we close the connection.
- Now the question i have is why instead of relying on unique message , we rely on whether the client to which we sent a ping has responded with a pong irrespective of the data , if it is sending us something that means the connection is alive.
- Recently i had to create a hybrid ping pong mechanism which uses the client which is connected to my websocket server, i use it's id to map it to the ping task and if i receive any pong from that particular client id i mark the connection as keep alive.
- In normal ping pong, if a client responds with a different message in pong , it doesnt acknowledge it and marks the connection as closed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement