跳转至

ws

WebsocketClientConnection 🔗

Bases: WebsocketConnectionMixin[WebsocketClientInfo]

Websocket 客户端连接

Source code in graia/ariadne/connection/ws.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@t.apply
class WebsocketClientConnection(WebsocketConnectionMixin[WebsocketClientInfo]):
    """Websocket 客户端连接"""

    dependencies = {AiohttpClientInterface}
    http_interface: AiohttpClientInterface

    @property
    def stages(self):
        return {"blocking"}

    async def launch(self, mgr: Launart) -> None:
        self.http_interface = mgr.get_interface(AiohttpClientInterface)
        config = self.info
        async with self.stage("blocking"):
            rider = self.http_interface.websocket(
                str(
                    (URL(config.host) / "all").with_query(
                        {"qq": config.account, "verifyKey": config.verify_key}
                    )
                ),
                heartbeat=30.0,
            )
            await wait_fut(
                [rider.use(self), mgr.status.wait_for_sigexit()],
                return_when=asyncio.FIRST_COMPLETED,
            )

    @t.on(WebsocketConnectEvent)
    async def _(self, io: AbstractWebsocketIO) -> None:  # start authenticate
        self.ws_io = io
        self.status.alive = True

WebsocketServerConnection 🔗

Bases: WebsocketConnectionMixin[WebsocketServerInfo]

Websocket 服务器连接

Source code in graia/ariadne/connection/ws.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
@t.apply
class WebsocketServerConnection(WebsocketConnectionMixin[WebsocketServerInfo]):
    """Websocket 服务器连接"""

    dependencies = {AbstractRouter}

    def __init__(self, info: WebsocketServerInfo) -> None:
        super().__init__(info)
        self.declares.append(WebsocketEndpoint(self.info.path))

    async def launch(self, mgr: Launart) -> None:
        router = mgr.get_interface(AbstractRouter)
        router.use(self)

    @t.on(WebsocketConnectEvent)
    async def _(self, io: AbstractWebsocketIO) -> None:
        req: HttpRequest = await io.extra(HttpRequest)
        for k, v in self.info.headers.items():
            if req.headers.get(k) != v:
                return await io.extra(WSConnectionClose)
        for k, v in self.info.params.items():
            if req.query_params.get(k) != v:
                return await io.extra(WSConnectionClose)
        await io.extra(WSConnectionAccept)
        await io.send(
            {
                "syncId": "#",
                "command": "verify",
                "content": {
                    "verifyKey": self.info.verify_key,
                    "sessionKey": None,
                    "qq": self.info.account,
                },
            }
        )
        self.ws_io = io