
























@@ -15,13 +15,19 @@ type SendInputNotifyFn = (
1515inputSecond: number,
1616) => Promise<unknown>;
171718-/** Refresh every 50s for the QQ API's 60s input-notify window. */
19-const TYPING_INTERVAL_MS = 50_000;
20-export const TYPING_INPUT_SECOND = 60;
18+/** Refresh every 5s for the QQ API's 10s input-notify window. */
19+const TYPING_INTERVAL_MS = 5_000;
20+export const TYPING_INPUT_SECOND = 10;
21+const QQ_C2C_PASSIVE_REPLY_LIMIT = 5;
22+const INITIAL_TYPING_NOTIFY_COUNT = 1;
23+const FINAL_REPLY_RESERVE_COUNT = 1;
24+export const TYPING_RENEWAL_LIMIT =
25+QQ_C2C_PASSIVE_REPLY_LIMIT - INITIAL_TYPING_NOTIFY_COUNT - FINAL_REPLY_RESERVE_COUNT;
21262227export class TypingKeepAlive {
2328private timer: ReturnType<typeof setInterval> | null = null;
2429private stopped = false;
30+private renewalsRemaining = TYPING_RENEWAL_LIMIT;
25312632constructor(
2733private readonly getToken: () => Promise<string>,
@@ -62,18 +68,35 @@ export class TypingKeepAlive {
6268private async send(): Promise<void> {
6369try {
6470const token = await this.getToken();
65-await this.sendInputNotify(token, this.openid, this.msgId, TYPING_INPUT_SECOND);
66-this.log?.debug?.(`Typing keep-alive sent to ${this.openid}`);
71+await this.sendAttempt(token);
6772} catch (err) {
6873try {
6974this.clearCache();
7075const token = await this.getToken();
71-await this.sendInputNotify(token, this.openid, this.msgId, TYPING_INPUT_SECOND);
76+await this.sendAttempt(token);
7277} catch {
7378this.log?.debug?.(
7479`Typing keep-alive failed for ${this.openid}: ${formatErrorMessage(err)}`,
7580);
7681}
7782}
7883}
84+85+private async sendAttempt(token: string): Promise<void> {
86+if (this.stopped || this.renewalsRemaining <= 0) {
87+this.stop();
88+return;
89+}
90+91+this.renewalsRemaining--;
92+try {
93+await this.sendInputNotify(token, this.openid, this.msgId, TYPING_INPUT_SECOND);
94+this.log?.debug?.(`Typing keep-alive sent to ${this.openid}`);
95+} finally {
96+if (this.renewalsRemaining <= 0) {
97+this.log?.debug?.(`Typing keep-alive budget exhausted for ${this.openid}`);
98+this.stop();
99+}
100+}
101+}
79102}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。