websocket_connections.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:monitor 作者: ReCodEx 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号