diff --git a/lib/client.js b/lib/client.js index 929ee1c..43ae5f1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -39,6 +39,9 @@ class ClawClient { this._pingTimer = null; this._awaitingPong = false; + // WS 连续失败计数(open 时清零) + this._wsFailCount = 0; + // systemd watchdog this._sdTimer = null; @@ -131,6 +134,7 @@ class ClawClient { ws.on('open', () => { log.info('clawd', 'WebSocket 已连接'); this._backoff = 1_000; + this._wsFailCount = 0; // 连接成功,重置失败计数 this._sendConnect(); this._startPing(); // 显示由 _onConnected 根据 status 设置,不在此处提前 showTime @@ -152,7 +156,15 @@ class ClawClient { this._clearHeartbeat(); this._clearPing(); if (!this._stopped) { - log.warn('clawd', `连接断开 (${code}),${this._backoff / 1000}s 后重连...`); + this._wsFailCount++; + log.warn('clawd', `连接断开 (${code}),失败次数=${this._wsFailCount},${this._backoff / 1000}s 后重连...`); + if (this._wsFailCount >= 3) { + if (hasInternet()) { + led.display.showErr('a'); // 有网但 VPS 不可达 + } else { + led.display.showAP(); // 无网,AP 模式 + } + } setTimeout(() => this._connect(), this._backoff); this._backoff = Math.min(this._backoff * 2, MAX_BACKOFF_MS); } diff --git a/lib/led.js b/lib/led.js index 319567b..ff9a09b 100644 --- a/lib/led.js +++ b/lib/led.js @@ -108,6 +108,18 @@ class Display { log.info('display', '显示屏 → AP'); } + /** + * 网络错误 → 显示错误码(ERR + 单字母代码) + * code='a' → ERRa(网络正常但 VPS 不可达) + * code='b' → ERRb(预留) + */ + showErr(code = 'a') { + this._stopBlink(); + const s = ('ERR' + code).slice(0, 4); + this._write('#m3' + s); + log.info('display', `显示屏 → ${s}`); + } + /** 网络已连接 → 显示时间 */ showTime() { this._stopBlink();