ws.py 文件源码

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

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


问题


面经


文章

微信
公众号

扫码关注公众号