connection_ws
本模块实现了 OneBot Connect 的 正反向 WebSocket 的部分以及 WebSocket 连接的基类。
ClientWSProtocol
Bases: WSProtocol
客户端 WS 协议类。
包装 aiohttp.ClientWebSocketResponse。
Source code in src/pylibob/connection_ws.py
ServerWSProtocol
Bases: WSProtocol
服务器 WS 协议类。
包装 starlette.websockets.WebSocket。
Source code in src/pylibob/connection_ws.py
WSProtocol
Bases: ABC
抽象 WS 协议类。
用于内部的统一处理。
Source code in src/pylibob/connection_ws.py
receive()
abstractmethod
async
send_json(data)
abstractmethod
async
send_msgpack(data)
abstractmethod
async
WebSocket
Bases: WebSocketConnection, ServerConnection
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
host |
str
|
WebSocket 服务器监听 IP |
port |
int
|
WebSocket 服务器端口 |
enable_heartbeat |
bool
|
是否启用心跳 |
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
Source code in src/pylibob/connection_ws.py
__init__(*, access_token=None, host='0.0.0.0', port=8080, enable_heartbeat=True, heartbeat_interval=5000)
初始化正向 WebSocket 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str | None
|
访问令牌 |
None
|
host |
str
|
WebSocket 服务器监听 IP |
'0.0.0.0'
|
port |
int
|
WebSocket 服务器端口 |
8080
|
enable_heartbeat |
bool
|
是否启用心跳 |
True
|
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
5000
|
Source code in src/pylibob/connection_ws.py
WebSocketConnection
Bases: Connection
WebSocket 连接基类。
WebSocket 会在启用心跳时,启动心跳服务,每隔 heartbeat_interval 毫秒发送心跳。
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
enable_heartbeat |
bool
|
是否启用心跳 |
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
Source code in src/pylibob/connection_ws.py
152 153 154 155 156 157 158 159 160 161 162 163 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
__init__(*, access_token=None, enable_heartbeat=True, heartbeat_interval=5000)
初始化 WebSocket 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str | None
|
访问令牌 |
None
|
enable_heartbeat |
bool
|
是否启用心跳 |
True
|
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
5000
|
Source code in src/pylibob/connection_ws.py
listen_ws(ws)
async
监听 WebSocket 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws |
WSProtocol
|
WebSocket 协议实例 |
required |
Source code in src/pylibob/connection_ws.py
WebSocketReverse
Bases: ClientConnection, WebSocketConnection
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
reconnect_interval |
int
|
重连间隔。单位: 毫秒 |
enable_heartbeat |
bool
|
是否启用心跳 |
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
Source code in src/pylibob/connection_ws.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
__init__(url, *, access_token=None, reconnect_interval=5000, enable_heartbeat=True, heartbeat_interval=5000)
初始化反向 WebSocket 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str | None
|
访问令牌 |
None
|
reconnect_interval |
int
|
重连间隔。单位: 毫秒 |
5000
|
enable_heartbeat |
bool
|
是否启用心跳 |
True
|
heartbeat_interval |
int
|
心跳间隔。单位: 毫秒 |
5000
|