Skip to content

Commit 1f882b5

Browse files
committed
update:规范websocket连接
1 parent 5f3d162 commit 1f882b5

File tree

3 files changed

+0
-104
lines changed

3 files changed

+0
-104
lines changed

src/handlers/websocket_handler.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from ..core.connection_manager import connection_manager
1212
from ..utils.logger import get_logger
1313
from ..utils.jsonrpc import (
14-
JSONRPCProtocol,
15-
create_connection_established_message,
1614
create_tool_not_connected_error,
1715
create_forward_failed_error,
1816
)
@@ -26,78 +24,6 @@ class WebSocketHandler:
2624
def __init__(self):
2725
pass
2826

29-
async def handle_tool_connection(
30-
self, websocket: WebSocketServerProtocol, path: str
31-
):
32-
"""处理工具端连接"""
33-
try:
34-
# 解析URL参数获取agentId
35-
agent_id = self._extract_agent_id(path)
36-
if not agent_id:
37-
await websocket.close(1008, "缺少agentId参数")
38-
return
39-
40-
# 注册连接
41-
await connection_manager.register_tool_connection(agent_id, websocket)
42-
43-
# 发送连接确认消息
44-
connection_message = create_connection_established_message(
45-
agent_id, "工具端连接已建立"
46-
)
47-
await websocket.send(connection_message)
48-
49-
logger.info(f"工具端连接已建立: {agent_id}")
50-
51-
# 处理消息
52-
async for message in websocket:
53-
await self._handle_tool_message(agent_id, message)
54-
55-
except ConnectionClosed:
56-
logger.info(
57-
f"工具端连接已关闭: {agent_id if 'agent_id' in locals() else 'unknown'}"
58-
)
59-
except Exception as e:
60-
logger.error(f"处理工具端连接时发生错误: {e}")
61-
finally:
62-
if "agent_id" in locals():
63-
await connection_manager.unregister_tool_connection(agent_id)
64-
65-
async def handle_robot_connection(
66-
self, websocket: WebSocketServerProtocol, path: str
67-
):
68-
"""处理小智端连接"""
69-
try:
70-
# 解析URL参数获取agentId
71-
agent_id = self._extract_agent_id(path)
72-
if not agent_id:
73-
await websocket.close(1008, "缺少agentId参数")
74-
return
75-
76-
# 注册连接
77-
await connection_manager.register_robot_connection(agent_id, websocket)
78-
79-
# 发送连接确认消息
80-
connection_message = create_connection_established_message(
81-
agent_id, "小智端连接已建立"
82-
)
83-
await websocket.send(connection_message)
84-
85-
logger.info(f"小智端连接已建立: {agent_id}")
86-
87-
# 处理消息
88-
async for message in websocket:
89-
await self._handle_robot_message(agent_id, message)
90-
91-
except ConnectionClosed:
92-
logger.info(
93-
f"小智端连接已关闭: {agent_id if 'agent_id' in locals() else 'unknown'}"
94-
)
95-
except Exception as e:
96-
logger.error(f"处理小智端连接时发生错误: {e}")
97-
finally:
98-
if "agent_id" in locals():
99-
await connection_manager.unregister_robot_connection(agent_id)
100-
10127
async def _handle_tool_message(self, agent_id: str, message: str):
10228
"""处理工具端消息"""
10329
try:

src/server.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from .utils.aes_utils import decrypt
1313
from .utils.jsonrpc import (
1414
JSONRPCProtocol,
15-
create_connection_established_message,
16-
create_authentication_error,
1715
)
1816
from src.utils.util import get_local_ip
1917
from contextlib import asynccontextmanager
@@ -149,13 +147,6 @@ async def websocket_tool_endpoint(websocket: WebSocket):
149147
try:
150148
# 注册连接
151149
await connection_manager.register_tool_connection(agent_id, websocket)
152-
153-
# 发送连接确认消息
154-
connection_message = create_connection_established_message(
155-
agent_id, "工具端连接已建立"
156-
)
157-
await websocket.send_text(connection_message)
158-
159150
logger.info(f"工具端连接已建立: {agent_id}")
160151

161152
# 处理消息
@@ -190,12 +181,6 @@ async def websocket_robot_endpoint(websocket: WebSocket):
190181
# 注册连接
191182
await connection_manager.register_robot_connection(agent_id, websocket)
192183

193-
# 发送连接确认消息
194-
connection_message = create_connection_established_message(
195-
agent_id, "小智端连接已建立"
196-
)
197-
await websocket.send_text(connection_message)
198-
199184
logger.info(f"小智端连接已建立: {agent_id}")
200185

201186
# 处理消息

src/utils/jsonrpc.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,6 @@ def is_notification(json_str: str) -> bool:
178178
return request is not None and request.id is None
179179

180180

181-
# 便捷函数
182-
def create_connection_established_message(
183-
agent_id: str, message: str = "连接已建立"
184-
) -> str:
185-
"""创建连接建立成功的消息"""
186-
response = JSONRPCProtocol.create_success_response(
187-
result={
188-
"type": "connection_established",
189-
"message": message,
190-
"agent_id": agent_id,
191-
}
192-
)
193-
return JSONRPCProtocol.to_json(response, ensure_ascii=False)
194-
195-
196181
def create_tool_not_connected_error(
197182
request_id: Optional[Union[str, int]] = None, agent_id: Optional[str] = None
198183
) -> str:

0 commit comments

Comments
 (0)