def connection_handler(self, websocket, path):
"""
Internal asyncio.coroutine function for handling one websocket request.
:param websocket: Socket with request
:param path: Requested path of socket (not used)
:return: Returns when socket is closed or poison pill is found in message queue
from ClientConnections.
"""
wanted_id = None
try:
wanted_id = yield from websocket.recv()
queue = self._connections.add_client(wanted_id)
self._logger.debug("websocket server: got client for channel '{}'".format(wanted_id))
while True:
# wait for message
result = yield from queue.get()
if not result:
break
self._logger.debug("websocket server: message '{}' for channel '{}'".format(result, wanted_id))
# send message to client
yield from websocket.send(result)
self._logger.debug("websocket server: message sent to channel '{}'".format(wanted_id))
except websockets.ConnectionClosed:
self._logger.info("websocket server: connection closed for channel '{}'". format(wanted_id))
finally:
self._connections.remove_client(wanted_id, queue)
评论列表
文章目录