-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnector.ts
More file actions
43 lines (38 loc) · 1.17 KB
/
connector.ts
File metadata and controls
43 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {
WebSocketServerEndpoint,
ConnectorCenter,
WebSocketEndpointServer,
SSEEndpointServer,
SSEServerEndpoint,
} from '@opentiny/tiny-agent-mcp-connector';
import type { Request } from 'express';
import type { WebSocket } from 'ws';
export function createConnector() {
const connectorCenter = new ConnectorCenter<WebSocketServerEndpoint>();
const webSocketEndpointServer = new WebSocketEndpointServer({ noServer: true }, connectorCenter);
webSocketEndpointServer.start();
const websocketConnectionHandler = async (ws: WebSocket, req: Request) => {
webSocketEndpointServer.wss.emit('connection', ws, req);
};
return {
connectorCenter,
webSocketEndpointServer,
websocketConnectionHandler,
};
}
export function createSSEConnector() {
const connectorCenter = new ConnectorCenter<SSEServerEndpoint>();
const port = 8082;
const sseEndpointServer = new SSEEndpointServer({ port }, connectorCenter);
sseEndpointServer.start();
try {
sseEndpointServer.start();
} catch (error) {
console.error(`Failed to start SSE endpoint server on port ${port}:`, error);
throw error;
}
return {
connectorCenter,
sseEndpointServer,
};
}