DG-Lab WebSocket 服务端
DGLabWSServer ¶
DGLabWSServer(host: Union[str, Sequence[str]], port: Optional[int] = None, heartbeat_interval: float = None, **kwargs)
DG-Lab WebSocket 服务器
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host |
Union[str, Sequence[str]]
|
WebSocket 服务器绑定的接口 |
required |
port |
Optional[int]
|
监听端口 |
None
|
heartbeat_interval |
float
|
心跳包发送间隔(秒) |
None
|
kwargs |
:class: |
{}
|
Source code in pydglab_ws/server/server.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
client_id_to_target_id
property
¶
client_id_to_target_id: Dict[UUID4, UUID4]
client_id 到 target_id 的映射
target_id_to_client_id
property
¶
target_id_to_client_id: Dict[UUID4, UUID4]
target_id 到 client_id 的映射
uuid_to_ws
property
¶
uuid_to_ws: Dict[UUID4, WebSocketServerProtocol]
所有的 WebSocket 客户端 ID(包含终端与 App)到 WebSocket 连接对象的映射
new_local_client ¶
new_local_client(max_queue: int = 2 ** 5) -> DGLabLocalClient
创建新的本地终端 DGLabLocalClient,记录并返回
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_queue |
int
|
终端消息队列最大长度 |
2 ** 5
|
Returns:
| Type | Description |
|---|---|
DGLabLocalClient
|
创建好的本地终端对象 |
Source code in pydglab_ws/server/server.py
123 124 125 126 127 128 129 130 131 132 133 134 135 | |
remove_local_client
async
¶
remove_local_client(client_id: UUID4) -> bool
移除已连接的本地终端,并通知 App 终端已掉线
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id |
UUID4
|
要移除的本地终端 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
如果该终端并没有与服务端连接,返回 |
Source code in pydglab_ws/server/server.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
add_receive_callback ¶
add_receive_callback(message_type: Literal[BIND, MSG], func: Callable[[WebSocketMessage, bool], Any]) -> bool
添加回调函数,在收到指定类型的消息后调用
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_type |
Literal[BIND, MSG]
|
消息类型,仅支持 |
required |
func |
Callable[[WebSocketMessage, bool], Any]
|
回调函数,传入消息数据和服务端处理结果,支持异步函数 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
是否有找到消息类型 |
Source code in pydglab_ws/server/server.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
remove_receive_callback ¶
remove_receive_callback(message_type: Literal[BIND, MSG], func: Callable[[WebSocketMessage, bool], Any]) -> bool
移除在收到指定类型的消息后调用的回调函数
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_type |
Literal[BIND, MSG]
|
消息类型,仅支持 |
required |
func |
Callable[[WebSocketMessage, bool], Any]
|
回调函数,传入消息数据和服务端处理结果,支持异步函数 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
是否有找到消息类型和回调函数 |
Source code in pydglab_ws/server/server.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
add_connection_callback ¶
add_connection_callback(mode: Literal['new_connect', 'disconnect'], func: Callable[[UUID4, WebSocketServerProtocol], Any]) -> bool
添加回调函数,在新的 WebSocket 连接建立时(新客户端)或连接断开时调用
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode |
Literal['new_connect', 'disconnect']
|
类型, |
required |
func |
Callable[[UUID4, WebSocketServerProtocol], Any]
|
回调函数,传入 终端 / App 的 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in pydglab_ws/server/server.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
remove_connection_callback ¶
remove_connection_callback(mode: Literal['new_connect', 'disconnect'], func: Callable[[UUID4, WebSocketServerProtocol], Any]) -> bool
删除在新的 WebSocket 连接建立时(新客户端)或连接断开时调用的回调函数
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode |
Literal['new_connect', 'disconnect']
|
类型, |
required |
func |
Callable[[UUID4, WebSocketServerProtocol], Any]
|
回调函数,传入 终端 / App 的 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in pydglab_ws/server/server.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |