dsh-message-gateway 0.1.4 → 0.1.6
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/lib/index.js +30 -12
- package/lib/index.js.map +2 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -581,32 +581,45 @@ var QQBridge = class {
|
|
|
581
581
|
tokenExpiresAt = 0;
|
|
582
582
|
/** 单聊流式/群聊定稿发送器。 */
|
|
583
583
|
sender;
|
|
584
|
+
/** 连续重连次数(退避用,READY 后重置)。 */
|
|
585
|
+
reconnectAttempts = 0;
|
|
584
586
|
recentMsgIds = /* @__PURE__ */ new Set();
|
|
585
587
|
status = { state: "idle", detail: "", connectedAt: null };
|
|
586
588
|
setStatus(state, detail = "") {
|
|
587
589
|
this.status = { state, detail, connectedAt: state === "connected" ? Date.now() : this.status.connectedAt };
|
|
588
590
|
this.callbacks.onStatus(this.status);
|
|
589
591
|
}
|
|
590
|
-
/** 启动:先取 access_token,再取 WSS
|
|
592
|
+
/** 启动:先取 access_token,再取 WSS 接入点,最后连网关;失败自动重试(退避)。 */
|
|
591
593
|
start() {
|
|
592
594
|
if (!this.stopped && this.ws !== null) return;
|
|
593
595
|
this.stopped = false;
|
|
594
596
|
this.setStatus("connecting", "access token");
|
|
595
|
-
void this.
|
|
597
|
+
void this.tryStart();
|
|
598
|
+
}
|
|
599
|
+
async tryStart() {
|
|
600
|
+
while (!this.stopped && this.ws === null) {
|
|
601
|
+
const token = await this.getAccessToken();
|
|
596
602
|
if (this.stopped) return;
|
|
597
603
|
if (token === null) {
|
|
598
|
-
|
|
599
|
-
|
|
604
|
+
const delay = Math.min(3e3 * 2 ** this.reconnectAttempts, 6e4);
|
|
605
|
+
this.reconnectAttempts += 1;
|
|
606
|
+
this.setStatus("connecting", `token retry ${delay}ms`);
|
|
607
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
608
|
+
continue;
|
|
600
609
|
}
|
|
601
610
|
this.setStatus("connecting", "gateway");
|
|
602
611
|
const url = await this.getGatewayUrl();
|
|
603
612
|
if (this.stopped) return;
|
|
604
613
|
if (url === null) {
|
|
605
|
-
|
|
606
|
-
|
|
614
|
+
const delay = Math.min(3e3 * 2 ** this.reconnectAttempts, 6e4);
|
|
615
|
+
this.reconnectAttempts += 1;
|
|
616
|
+
this.setStatus("connecting", `gateway retry ${delay}ms`);
|
|
617
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
618
|
+
continue;
|
|
607
619
|
}
|
|
608
620
|
this.connect(url);
|
|
609
|
-
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
610
623
|
}
|
|
611
624
|
async getAccessToken() {
|
|
612
625
|
const controller = new AbortController();
|
|
@@ -681,16 +694,19 @@ var QQBridge = class {
|
|
|
681
694
|
this.clearHeartbeat();
|
|
682
695
|
this.ws = null;
|
|
683
696
|
if (!this.stopped) {
|
|
684
|
-
|
|
697
|
+
this.setStatus("connecting", "reconnect");
|
|
698
|
+
const delay = Math.min(3e3 * 2 ** this.reconnectAttempts, 6e4);
|
|
699
|
+
this.reconnectAttempts += 1;
|
|
700
|
+
console.warn(`[dsh-message-gateway] qq gateway closed, reconnect in ${delay}ms`);
|
|
685
701
|
setTimeout(() => {
|
|
686
702
|
if (this.stopped) return;
|
|
687
|
-
void this.
|
|
688
|
-
if (this.stopped ||
|
|
703
|
+
void this.getAccessToken().then(async (token) => {
|
|
704
|
+
if (this.stopped || token === null) return;
|
|
689
705
|
const u = await this.getGatewayUrl();
|
|
690
706
|
if (this.stopped || u === null) return;
|
|
691
707
|
this.connect(u);
|
|
692
708
|
});
|
|
693
|
-
},
|
|
709
|
+
}, delay);
|
|
694
710
|
}
|
|
695
711
|
};
|
|
696
712
|
}
|
|
@@ -715,6 +731,7 @@ var QQBridge = class {
|
|
|
715
731
|
}
|
|
716
732
|
case 0: {
|
|
717
733
|
if (payload.t === "READY") {
|
|
734
|
+
this.reconnectAttempts = 0;
|
|
718
735
|
this.setStatus("connected", "qq");
|
|
719
736
|
console.log("[dsh-message-gateway] qq READY");
|
|
720
737
|
} else if (payload.t === "C2C_MESSAGE_CREATE") {
|
|
@@ -725,7 +742,8 @@ var QQBridge = class {
|
|
|
725
742
|
break;
|
|
726
743
|
}
|
|
727
744
|
case 9: {
|
|
728
|
-
console.warn("[dsh-message-gateway] qq identify rejected (INVALID_SESSION)
|
|
745
|
+
console.warn("[dsh-message-gateway] qq identify rejected (INVALID_SESSION), refreshing token");
|
|
746
|
+
this.accessToken = null;
|
|
729
747
|
this.ws?.close();
|
|
730
748
|
break;
|
|
731
749
|
}
|