Browse Source

Add DiscordWebSocket.latency to measure discord heartbeat latency.

pull/664/merge
Rapptz 8 years ago
parent
commit
ceafae0ab2
  1. 9
      discord/gateway.py

9
discord/gateway.py

@ -63,6 +63,7 @@ class KeepAliveHandler(threading.Thread):
self.msg = 'Keeping websocket alive with sequence %s.'
self._stop_ev = threading.Event()
self._last_ack = time.time()
self._last_send = time.time()
def run(self):
while not self._stop_ev.wait(self.interval):
@ -88,6 +89,8 @@ class KeepAliveHandler(threading.Thread):
f.result()
except Exception:
self.stop()
else:
self._last_send = time.time()
def get_payload(self):
return {
@ -408,6 +411,12 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
for index in reversed(removed):
del self._dispatch_listeners[index]
@property
def latency(self):
"""float: Measures latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds."""
heartbeat = self._keep_alive
return float('inf') if heartbeat is None else heartbeat._last_ack - heartbeat._last_send
def _can_handle_close(self, code):
return code not in (1000, 4004, 4010, 4011)

Loading…
Cancel
Save