@yeaft/webchat-agent 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/connection.js +12 -9
  2. package/package.json +1 -1
package/connection.js CHANGED
@@ -95,6 +95,16 @@ async function parseMessage(data) {
95
95
 
96
96
  async function handleMessage(msg) {
97
97
  switch (msg.type) {
98
+ case 'ping':
99
+ // 应用层心跳:server 发 ping,agent 回 pong
100
+ sendToServer({ type: 'pong' });
101
+ return;
102
+
103
+ case 'pong':
104
+ // 应用层心跳响应:更新 lastPongAt
105
+ ctx.lastPongAt = Date.now();
106
+ return;
107
+
98
108
  case 'registered':
99
109
  if (msg.sessionKey) {
100
110
  ctx.sessionKey = decodeKey(msg.sessionKey);
@@ -476,13 +486,6 @@ export function startAgentHeartbeat() {
476
486
  stopAgentHeartbeat();
477
487
  ctx.lastPongAt = Date.now();
478
488
 
479
- // 监听 pong 帧
480
- if (ctx.ws) {
481
- ctx.ws.on('pong', () => {
482
- ctx.lastPongAt = Date.now();
483
- });
484
- }
485
-
486
489
  ctx.agentHeartbeatTimer = setInterval(() => {
487
490
  if (!ctx.ws || ctx.ws.readyState !== WebSocket.OPEN) return;
488
491
 
@@ -495,7 +498,7 @@ export function startAgentHeartbeat() {
495
498
  }
496
499
 
497
500
  try {
498
- ctx.ws.ping();
501
+ sendToServer({ type: 'ping' });
499
502
  } catch (e) {
500
503
  console.warn('[Heartbeat] Failed to send ping:', e.message);
501
504
  }
@@ -539,7 +542,7 @@ export function connect() {
539
542
  ctx.ws.on('open', () => {
540
543
  console.log('Connected to server, waiting for auth challenge...');
541
544
  clearTimeout(ctx.reconnectTimer);
542
- // 启动 agent 端心跳: 每 25 秒发一次 ping
545
+ // 启动 agent 端心跳: 每 25 秒发一次应用层 ping
543
546
  startAgentHeartbeat();
544
547
  });
545
548
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",