async def _handle(self, scope, receive, send, request_context): connection_context = AsgiConnectionContext(scope, receive, send, request_context) logger.debug("Handle incoming websocket connection") await connection_context.accept() await self.on_open(connection_context) pending = set() while True: try: if connection_context.closed: raise ConnectionClosedException() message = await connection_context.receive() except ConnectionClosedException: break finally: if pending: (_, pending) = await wait(pending, timeout=0, loop=self.loop) task = ensure_future(self.on_message(connection_context, message), loop=self.loop) pending.add(task) self.on_close(connection_context) for task in pending: task.cancel()
async def _handle(self, ws, request_context): connection_context = TornadoConnectionContext(ws, request_context) await self.on_open(connection_context) pending = set() while True: message = None try: if connection_context.closed: raise ConnectionClosedException() message = connection_context.receive() except ConnectionClosedException: break except QueueEmpty: pass finally: if pending: (_, pending) = await wait(pending, timeout=0, loop=self.loop) if message: task = create_task(self.on_message(connection_context, message)) pending.add(task) else: await sleep(NO_MSG_DELAY) self.on_close(connection_context) for task in pending: task.cancel()
async def receive(self): assert self.client_state == WebSocketState.CONNECTED message = await self._receive() if message["type"] == "websocket.disconnect": self.client_state = WebSocketState.DISCONNECTED raise ConnectionClosedException() else: return message["text"]
async def _handle(self, ws, request_context=None): connection_context = TornadoConnectionContext(ws, request_context) await self.on_open(connection_context) while True: message = None try: if connection_context.closed: raise ConnectionClosedException() message = await connection_context.receive() except QueueEmpty: pass except ConnectionClosedException: break if message: self.on_message(connection_context, message) else: await sleep(NO_MSG_DELAY) await self.on_close(connection_context)
def handle(self, ws, request_context=None): connection_context = GeventConnectionContext(ws, request_context) self.on_open(connection_context) while True: try: if connection_context.closed: raise ConnectionClosedException() message = connection_context.receive() except ConnectionClosedException: self.on_close(connection_context) return if message: msg = json.loads(message) if msg.get('type', '') == 'start': Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') self.on_message(connection_context, message)
async def _handle(self, ws, request_context): connection_context = TornadoConnectionContext(ws, request_context) await self.on_open(connection_context) pending = set() while True: try: if connection_context.closed: raise ConnectionClosedException() message = await connection_context.receive() except ConnectionClosedException: break finally: if pending: (_, pending) = await wait(pending, timeout=0, loop=self.loop) task = ensure_future(self.on_message(connection_context, message), loop=self.loop) pending.add(task) self.on_close(connection_context) for task in pending: task.cancel()
async def receive(self): try: return self.ws.recv_nowait() except WebSocketClosedError: raise ConnectionClosedException()
async def receive(self): try: msg = await self.ws.recv() return msg except WebSocketClosedError: raise ConnectionClosedException()