MCPcopy Create free account
hub / github.com/echoVic/blade-code / handleConnection

Method handleConnection

src/mcp/server/MCPServer.ts:105–129  ·  view source on GitHub ↗

* 处理 WebSocket 连接

(ws: WebSocket, request: any)

Source from the content-addressed store, hash-verified

103 * 处理 WebSocket 连接
104 */
105 private handleConnection(ws: WebSocket, request: any): void {
106 const clientId = `client-${Date.now()}-${Math.random()}`;
107 this.clients.set(clientId, ws);
108
109 console.log(`Client connected: ${clientId}`);
110
111 ws.on('message', async data => {
112 try {
113 const message = JSON.parse(data.toString());
114 await this.handleMessage(clientId, message);
115 } catch (error) {
116 this.sendError(clientId, -32700, 'Parse error', error);
117 }
118 });
119
120 ws.on('close', () => {
121 this.clients.delete(clientId);
122 console.log(`Client disconnected: ${clientId}`);
123 });
124
125 ws.on('error', error => {
126 console.error(`WebSocket error for ${clientId}:`, error);
127 this.clients.delete(clientId);
128 });
129 }
130
131 /**
132 * 处理 Stdio 连接

Callers 1

startMethod · 0.95

Calls 7

handleMessageMethod · 0.95
sendErrorMethod · 0.95
setMethod · 0.80
deleteMethod · 0.80
logMethod · 0.45
onMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected