def chat(ws):
"""Relay chat messages to and from clients.
"""
# Subscribe to messages on the specified channel.
channel = request.args.get('channel')
lag_tolerance_secs = float(request.args.get('tolerance', 0.1))
chat_backend.subscribe(ws, channel)
# Send heartbeat ping every 30s
# so Heroku won't close the connection
gevent.spawn(chat_backend.heartbeat, ws)
while not ws.closed:
# Sleep to prevent *constant* context-switches.
gevent.sleep(lag_tolerance_secs)
# Publish messages from client
message = ws.receive()
if message is not None:
channel, data = message.split(':', 1)
conn.publish(channel, data)
评论列表
文章目录