2022-01-22 15:23:07 +08:00
|
|
|
"""[websockets](https://websockets.readthedocs.io/) 驱动适配
|
|
|
|
|
|
|
|
```bash
|
|
|
|
nb driver install websockets
|
|
|
|
# 或者
|
|
|
|
pip install nonebot2[websockets]
|
|
|
|
```
|
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
本驱动仅支持客户端 WebSocket 连接
|
|
|
|
:::
|
|
|
|
|
|
|
|
FrontMatter:
|
|
|
|
sidebar_position: 4
|
|
|
|
description: nonebot.drivers.websockets 模块
|
|
|
|
"""
|
2021-12-22 16:53:55 +08:00
|
|
|
import logging
|
2021-12-26 14:20:09 +08:00
|
|
|
from functools import wraps
|
2021-12-26 13:42:13 +08:00
|
|
|
from contextlib import asynccontextmanager
|
2022-05-14 21:06:57 +08:00
|
|
|
from typing import Type, Union, AsyncGenerator
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
from nonebot.typing import overrides
|
|
|
|
from nonebot.log import LoguruHandler
|
|
|
|
from nonebot.drivers import Request, Response
|
2021-12-26 14:20:09 +08:00
|
|
|
from nonebot.exception import WebSocketClosed
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.drivers._block_driver import BlockDriver
|
|
|
|
from nonebot.drivers import WebSocket as BaseWebSocket
|
2022-01-22 15:23:07 +08:00
|
|
|
from nonebot.drivers import ForwardMixin, ForwardDriver, combine_driver
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2021-12-23 17:20:26 +08:00
|
|
|
try:
|
2021-12-26 14:20:09 +08:00
|
|
|
from websockets.exceptions import ConnectionClosed
|
2021-12-23 17:20:26 +08:00
|
|
|
from websockets.legacy.client import Connect, WebSocketClientProtocol
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError(
|
|
|
|
"Please install websockets by using `pip install nonebot2[websockets]`"
|
|
|
|
)
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
logger = logging.Logger("websockets.client", "INFO")
|
|
|
|
logger.addHandler(LoguruHandler())
|
|
|
|
|
|
|
|
|
2021-12-26 14:20:09 +08:00
|
|
|
def catch_closed(func):
|
|
|
|
@wraps(func)
|
|
|
|
async def decorator(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
except ConnectionClosed as e:
|
|
|
|
if e.rcvd_then_sent:
|
2022-05-14 21:06:57 +08:00
|
|
|
raise WebSocketClosed(e.rcvd.code, e.rcvd.reason) # type: ignore
|
2021-12-26 14:20:09 +08:00
|
|
|
else:
|
2022-05-14 21:06:57 +08:00
|
|
|
raise WebSocketClosed(e.sent.code, e.sent.reason) # type: ignore
|
2021-12-26 14:20:09 +08:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2021-12-23 17:20:26 +08:00
|
|
|
class Mixin(ForwardMixin):
|
2022-01-22 15:23:07 +08:00
|
|
|
"""Websockets Mixin"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
@property
|
|
|
|
@overrides(ForwardMixin)
|
|
|
|
def type(self) -> str:
|
|
|
|
return "websockets"
|
|
|
|
|
|
|
|
@overrides(ForwardMixin)
|
|
|
|
async def request(self, setup: Request) -> Response:
|
2021-12-23 17:20:26 +08:00
|
|
|
return await super(Mixin, self).request(setup)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
@overrides(ForwardMixin)
|
2021-12-26 13:42:13 +08:00
|
|
|
@asynccontextmanager
|
|
|
|
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
|
|
|
|
connection = Connect(
|
2021-12-22 16:53:55 +08:00
|
|
|
str(setup.url),
|
|
|
|
extra_headers=setup.headers.items(),
|
|
|
|
open_timeout=setup.timeout,
|
|
|
|
)
|
2021-12-26 13:42:13 +08:00
|
|
|
async with connection as ws:
|
|
|
|
yield WebSocket(request=setup, websocket=ws)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class WebSocket(BaseWebSocket):
|
2022-01-22 15:23:07 +08:00
|
|
|
"""Websockets WebSocket Wrapper"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
def __init__(self, *, request: Request, websocket: WebSocketClientProtocol):
|
|
|
|
super().__init__(request=request)
|
|
|
|
self.websocket = websocket
|
|
|
|
|
|
|
|
@property
|
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
def closed(self) -> bool:
|
|
|
|
return self.websocket.closed
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
async def accept(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
async def close(self, code: int = 1000, reason: str = ""):
|
|
|
|
await self.websocket.close(code, reason)
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
2021-12-26 14:20:09 +08:00
|
|
|
@catch_closed
|
2022-05-14 21:06:57 +08:00
|
|
|
async def receive(self) -> Union[str, bytes]:
|
|
|
|
msg = await self.websocket.recv()
|
|
|
|
return msg
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
@catch_closed
|
|
|
|
async def receive_text(self) -> str:
|
2021-12-22 16:53:55 +08:00
|
|
|
msg = await self.websocket.recv()
|
|
|
|
if isinstance(msg, bytes):
|
|
|
|
raise TypeError("WebSocket received unexpected frame type: bytes")
|
|
|
|
return msg
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
2021-12-26 14:20:09 +08:00
|
|
|
@catch_closed
|
2021-12-22 16:53:55 +08:00
|
|
|
async def receive_bytes(self) -> bytes:
|
|
|
|
msg = await self.websocket.recv()
|
|
|
|
if isinstance(msg, str):
|
|
|
|
raise TypeError("WebSocket received unexpected frame type: str")
|
|
|
|
return msg
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
2022-05-14 21:06:57 +08:00
|
|
|
async def send_text(self, data: str) -> None:
|
2021-12-22 16:53:55 +08:00
|
|
|
await self.websocket.send(data)
|
|
|
|
|
|
|
|
@overrides(BaseWebSocket)
|
|
|
|
async def send_bytes(self, data: bytes) -> None:
|
|
|
|
await self.websocket.send(data)
|
|
|
|
|
|
|
|
|
2022-01-22 15:23:07 +08:00
|
|
|
Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore
|
|
|
|
"""Websockets Driver"""
|