@yeaft/webchat-agent 0.0.26 → 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.
- package/connection.js +12 -9
- 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
|
-
|
|
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
|
|
545
|
+
// 启动 agent 端心跳: 每 25 秒发一次应用层 ping
|
|
543
546
|
startAgentHeartbeat();
|
|
544
547
|
});
|
|
545
548
|
|