Beispiel #1
0
    async def connectAsyncWS(headers):

        try:
            wsUri = "ws://" + RtConfig.host + ":" + str(
                RtConfig.port) + "/websocket"
            async with websockets.connect(
                    wsUri, extra_headers=headers,
                    max_size=1_000_000_000_000) as websocket:

                WebSocketClientHandler._ws = websocket
                WebSocketClientHandler.connecting = False
                WebSocketClientHandler.connected = True

                from xyz.redtorch.client.service.rpc.RpcClientProcessService import RpcClientProcessService
                RpcClientProcessService.onWsConnected()
                logger.info("连接已建立")

                async for message in websocket:
                    if isinstance(message, bytes):
                        from xyz.redtorch.client.service.rpc.RpcClientProcessService import RpcClientProcessService
                        RpcClientProcessService.processData(message)
                    else:
                        logger.warning("接收到非二进制消息")

        except:
            logger.error("WebSocket连接断开", exc_info=True)
        finally:
            from xyz.redtorch.client.service.rpc.RpcClientProcessService import RpcClientProcessService
            RpcClientProcessService.onWsClosed()
            WebSocketClientHandler._ws = None
            WebSocketClientHandler.connecting = False
            WebSocketClientHandler.connected = False
 def onClose(ws):
     from xyz.redtorch.client.service.rpc.RpcClientProcessService import RpcClientProcessService
     RpcClientProcessService.onWsClosed()
     WebSocketClientHandler._ws = None
     logger.error("会话关闭")