如何计算龙卷风中的网络延迟?
How to calculate to web latency in torando?
我正在使用 Tornado Web 套接字并想计算客户端的 Web 套接字延迟。我看过在tornado中使用ping/pong但是我没有看清楚,也没有很好的例子。
是否有发送ping/pong响应和计算网络响应的简单示例代码?
我已经使用下面的代码来计算 WebSocket 延迟。 Ping 将发送一个时间戳。在 pong 中,我们接收到 ping 时间戳,并通过从当前时间减去 ping 时间戳来计算延迟。
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
ping_errors = 0
# Send a ping packet with the timestamp
def send_ping():
global ping_errors
timestamp = time.time()
try:
self.ping(str(timestamp).encode('utf-8'))
ping_errors = 0
except tornado.websocket.WebSocketClosedError:
msg = "Web socket closed. Stopped ping."
logger.info(msg)
pinger.stop()
except Exception:
ping_errors += 1
msg = "Ping failed to send."
logger.error(msg, exc_info=True)
if ping_errors > 3:
msg = "Stopping ping."
logger.error(msg, exc_info=True)
pinger.stop()
interval = 10000 #ms
pinger = tornado.ioloop.PeriodicCallback(send_ping, interval)
pinger.start()
def on_pong(self, timestamp):
curr_time = time.time()
time_diff = curr_time - float(timestamp)
if time_diff < 0:
logger.info("Lost a ping packet")
return
logger.info("WebSocket Latency: {0}ms".format(
int(ceil(time_diff * 1000))))
def on_close(self):
print("WebSocket closed")
在处理程序中,
handlers = [
...,
(r'/ping', EchoWebSocket),
]
在javascript,
protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
ping_ws_url = protocol + window.location.host + "/ping";
ping_ws = new WebSocket(ping_ws_url);
我正在使用 Tornado Web 套接字并想计算客户端的 Web 套接字延迟。我看过在tornado中使用ping/pong但是我没有看清楚,也没有很好的例子。
是否有发送ping/pong响应和计算网络响应的简单示例代码?
我已经使用下面的代码来计算 WebSocket 延迟。 Ping 将发送一个时间戳。在 pong 中,我们接收到 ping 时间戳,并通过从当前时间减去 ping 时间戳来计算延迟。
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
ping_errors = 0
# Send a ping packet with the timestamp
def send_ping():
global ping_errors
timestamp = time.time()
try:
self.ping(str(timestamp).encode('utf-8'))
ping_errors = 0
except tornado.websocket.WebSocketClosedError:
msg = "Web socket closed. Stopped ping."
logger.info(msg)
pinger.stop()
except Exception:
ping_errors += 1
msg = "Ping failed to send."
logger.error(msg, exc_info=True)
if ping_errors > 3:
msg = "Stopping ping."
logger.error(msg, exc_info=True)
pinger.stop()
interval = 10000 #ms
pinger = tornado.ioloop.PeriodicCallback(send_ping, interval)
pinger.start()
def on_pong(self, timestamp):
curr_time = time.time()
time_diff = curr_time - float(timestamp)
if time_diff < 0:
logger.info("Lost a ping packet")
return
logger.info("WebSocket Latency: {0}ms".format(
int(ceil(time_diff * 1000))))
def on_close(self):
print("WebSocket closed")
在处理程序中,
handlers = [
...,
(r'/ping', EchoWebSocket),
]
在javascript,
protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
ping_ws_url = protocol + window.location.host + "/ping";
ping_ws = new WebSocket(ping_ws_url);