def _run(self):
"""Enter an infinite loop waiting for websocket packets"""
try:
while True:
payload = await self.recv()
await self.process(payload)
except (PayloadLengthExceeded, earl.DecodeError, json.JSONDecodeError):
await self.ws.close(CloseCodes.DECODE_ERROR, 'Decoding Error')
except asyncio.CancelledError:
log.info('[ws] Run task was cancelled')
await self.ws.close(1006, 'Task was cancelled')
except StopConnection as sc:
log.info('[ws] StopConncection: %r', sc)
sc_args = sc.args
c_code = sc.args[0]
if len(sc_args) == 1:
await self.ws.close(c_code, reason=CloseReasons.get(c_code))
elif len(sc_args) == 2:
await self.ws.close(c_code, reason=sc.args[1])
except websockets.ConnectionClosed as err:
log.info('[ws] Closed with %d, %r', err.code, err.reason)
except InvalidateSession as err:
resumable = err.args[0]
if not resumable:
await self._clean()
pass
except Exception as err:
log.error('Error while running', exc_info=True)
await self.ws.close(4000, f'Unexpected error: {err!r}')
await self._clean()
return
await self._clean()
if self.ws.open:
await self.ws.close(1000)
评论列表
文章目录