MCPcopy Create free account
hub / github.com/astercloud/aster / main

Function main

client-sdks/client-js/examples/event-subscription.ts:8–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6import { WebSocketClient, SubscriptionManager, isProgressEvent, isControlEvent, isMonitorEvent, isEventType } from "@aster/client-js";
7
8async function main() {
9 // 1. 创建 WebSocket 客户端
10 const ws = new WebSocketClient({
11 maxReconnectAttempts: 5,
12 reconnectDelay: 1000,
13 heartbeatInterval: 30000,
14 heartbeatTimeout: 10000,
15 });
16
17 // 2. 连接到 aster 服务器
18 try {
19 await ws.connect("ws://localhost:8080/ws");
20 console.log("✅ Connected to aster");
21 } catch (error) {
22 console.error("❌ Connection failed:", error);
23 return;
24 }
25
26 // 3. 创建订阅管理器
27 const subscriptionManager = new SubscriptionManager(ws);
28
29 // 4. 订阅所有三个通道
30 const subscription = subscriptionManager.subscribe(["progress", "control", "monitor"], {
31 agentId: "agent-123",
32 eventTypes: ["thinking", "text_chunk", "tool_start", "token_usage"],
33 });
34
35 // 5. 处理事件
36 try {
37 for await (const envelope of subscription) {
38 const event = envelope.event;
39
40 // 按通道分类处理
41 if (isProgressEvent(event)) {
42 handleProgressEvent(event);
43 } else if (isControlEvent(event)) {
44 handleControlEvent(event);
45 } else if (isMonitorEvent(event)) {
46 handleMonitorEvent(event);
47 }
48 }
49 } catch (error) {
50 console.error("❌ Event subscription error:", error);
51 }
52
53 // 6. 清理
54 subscription.unsubscribe();
55 ws.disconnect();
56}
57
58/**
59 * 处理 Progress Channel 事件

Callers 1

Calls 11

connectMethod · 0.95
subscribeMethod · 0.95
disconnectMethod · 0.95
isProgressEventFunction · 0.90
isControlEventFunction · 0.90
isMonitorEventFunction · 0.90
handleControlEventFunction · 0.85
logMethod · 0.80
handleProgressEventFunction · 0.70
handleMonitorEventFunction · 0.70
unsubscribeMethod · 0.45

Tested by

no test coverage detected