@ysgroup/core 0.1.18 → 0.1.19
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/package.json +1 -1
- package/src/index.ts +12 -0
- package/src/store.ts +74 -4
- package/src/version.ts +93 -0
- package/src/ws.ts +72 -19
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,12 +3,24 @@
|
|
|
3
3
|
export {
|
|
4
4
|
YsWsClient,
|
|
5
5
|
YsRpcError,
|
|
6
|
+
YsOfflineError,
|
|
7
|
+
YsConnectionLostError,
|
|
6
8
|
DEFAULT_CALL_TIMEOUT_MS,
|
|
9
|
+
DEFAULT_OFFLINE_WAIT_MS,
|
|
7
10
|
MIN_CALL_TIMEOUT_MS,
|
|
8
11
|
MAX_CALL_TIMEOUT_MS,
|
|
9
12
|
type Envelope,
|
|
13
|
+
type YsDisconnectInfo,
|
|
10
14
|
type YsWsClientOptions,
|
|
11
15
|
} from "./ws";
|
|
16
|
+
export {
|
|
17
|
+
currentEntry,
|
|
18
|
+
latestEntry,
|
|
19
|
+
hasNewVersion,
|
|
20
|
+
isStaleChunkError,
|
|
21
|
+
reloadPage,
|
|
22
|
+
takeReloadMessage,
|
|
23
|
+
} from "./version";
|
|
12
24
|
export { useYsStore, type YsConnectOptions, type YsCallOptions } from "./store";
|
|
13
25
|
export {
|
|
14
26
|
YsRest,
|
package/src/store.ts
CHANGED
|
@@ -2,12 +2,19 @@
|
|
|
2
2
|
import { defineStore } from "pinia";
|
|
3
3
|
import { ref, computed } from "vue";
|
|
4
4
|
import { sealLoginPassword } from "./password";
|
|
5
|
-
import {
|
|
5
|
+
import { hasNewVersion } from "./version";
|
|
6
|
+
import { YsRpcError, YsWsClient, type YsDisconnectInfo } from "./ws";
|
|
7
|
+
|
|
8
|
+
/** 前台每隔这么久检查一次是否有新版本前端;回到前台时距上次超过 1 分钟也检查。 */
|
|
9
|
+
const UPDATE_CHECK_MS = 5 * 60_000;
|
|
10
|
+
const UPDATE_RECHECK_ON_VISIBLE_MS = 60_000;
|
|
6
11
|
|
|
7
12
|
/** connect 的可选项。 */
|
|
8
13
|
export interface YsConnectOptions {
|
|
9
14
|
/** 所有业务调用的默认超时(毫秒),默认 10 秒,收在 1 秒~10 分钟之间。 */
|
|
10
15
|
callTimeoutMs?: number;
|
|
16
|
+
/** 断线时还没发出的请求最多等连接恢复多久(毫秒),默认 2 分钟,覆盖一次部署停机。 */
|
|
17
|
+
offlineWaitMs?: number;
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
/** call 的可选项。 */
|
|
@@ -72,12 +79,21 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
72
79
|
const accessToken = ref<string>(storedAccess);
|
|
73
80
|
const refreshToken = ref<string>(storedRefresh);
|
|
74
81
|
const connected = ref(false);
|
|
82
|
+
/** 本次断开(或首次连接前)开始的时间,毫秒;连上后为 0。服务状态组件据此决定提示级别。 */
|
|
83
|
+
const disconnectedAt = ref(0);
|
|
84
|
+
/** 本次断开是服务端主动重启(部署中)。 */
|
|
85
|
+
const restarting = ref(false);
|
|
86
|
+
/** 服务端已部署了新版本前端。 */
|
|
87
|
+
const updateAvailable = ref(false);
|
|
75
88
|
const appName = ref("");
|
|
76
89
|
const sessionMessage = ref("");
|
|
77
90
|
/** 差量订阅的表数据:table -> Map<id, doc> */
|
|
78
91
|
const tables = ref<Record<string, Map<string, Record<string, unknown>>>>({});
|
|
79
92
|
|
|
80
93
|
let ws: YsWsClient | null = null;
|
|
94
|
+
let updateTimer: ReturnType<typeof setInterval> | null = null;
|
|
95
|
+
let lastUpdateCheck = 0;
|
|
96
|
+
let checkingUpdate: Promise<boolean> | null = null;
|
|
81
97
|
let renewTimer: ReturnType<typeof setTimeout> | null = null;
|
|
82
98
|
let renewing: Promise<void> | null = null;
|
|
83
99
|
let renewRetryMs = 0;
|
|
@@ -93,16 +109,33 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
93
109
|
|
|
94
110
|
function connect(wsUrl: string, app: string, options: YsConnectOptions = {}) {
|
|
95
111
|
appName.value = app;
|
|
96
|
-
ws = new YsWsClient(wsUrl, {
|
|
112
|
+
ws = new YsWsClient(wsUrl, {
|
|
113
|
+
callTimeoutMs: options.callTimeoutMs,
|
|
114
|
+
offlineWaitMs: options.offlineWaitMs,
|
|
115
|
+
});
|
|
116
|
+
disconnectedAt.value = Date.now();
|
|
117
|
+
let everConnected = false;
|
|
97
118
|
ws.on("__connected", () => {
|
|
98
119
|
connected.value = true;
|
|
120
|
+
disconnectedAt.value = 0;
|
|
121
|
+
restarting.value = false;
|
|
122
|
+
// 断线重连后检查一次前端版本:部署会重启服务端,重连正是新版本上线的时刻。
|
|
123
|
+
if (everConnected) void checkUpdate();
|
|
124
|
+
everConnected = true;
|
|
99
125
|
if (
|
|
100
126
|
refreshToken.value &&
|
|
101
127
|
(renewOnConnect || !user.value || renewRetryMs > 0)
|
|
102
128
|
)
|
|
103
129
|
void renew();
|
|
104
130
|
});
|
|
105
|
-
ws.on("__disconnected", () =>
|
|
131
|
+
ws.on("__disconnected", (data: unknown) => {
|
|
132
|
+
connected.value = false;
|
|
133
|
+
if (!disconnectedAt.value) disconnectedAt.value = Date.now();
|
|
134
|
+
if ((data as YsDisconnectInfo | null)?.restarting)
|
|
135
|
+
restarting.value = true;
|
|
136
|
+
});
|
|
137
|
+
if (!updateTimer)
|
|
138
|
+
updateTimer = setInterval(() => void checkUpdate(), UPDATE_CHECK_MS);
|
|
106
139
|
ws.on("snapshot", (d: unknown) => applySnapshot(d as SnapshotData));
|
|
107
140
|
ws.on("delta", (d: unknown) => applyDelta(d as DeltaData));
|
|
108
141
|
ws.on("user_revoked", (d: unknown) =>
|
|
@@ -135,7 +168,10 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
135
168
|
|
|
136
169
|
/** 后台标签页的定时器会被浏览器推迟,回到前台时补一次续期。 */
|
|
137
170
|
function onVisible() {
|
|
138
|
-
if (document.visibilityState !== "visible"
|
|
171
|
+
if (document.visibilityState !== "visible") return;
|
|
172
|
+
if (Date.now() - lastUpdateCheck > UPDATE_RECHECK_ON_VISIBLE_MS)
|
|
173
|
+
void checkUpdate();
|
|
174
|
+
if (!refreshToken.value) return;
|
|
139
175
|
if (renewDueAt && Date.now() >= renewDueAt) void renew();
|
|
140
176
|
}
|
|
141
177
|
|
|
@@ -227,6 +263,35 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
227
263
|
setSession(result);
|
|
228
264
|
}
|
|
229
265
|
|
|
266
|
+
/**
|
|
267
|
+
* 检查服务端是否已有新版本前端,有就把 updateAvailable 置为 true(只升不降)。
|
|
268
|
+
* 后台标签页不检查;同时只跑一次。
|
|
269
|
+
*/
|
|
270
|
+
async function checkUpdate(): Promise<boolean> {
|
|
271
|
+
if (updateAvailable.value) return true;
|
|
272
|
+
if (
|
|
273
|
+
typeof document !== "undefined" &&
|
|
274
|
+
document.visibilityState === "hidden"
|
|
275
|
+
)
|
|
276
|
+
return false;
|
|
277
|
+
if (checkingUpdate) return checkingUpdate;
|
|
278
|
+
lastUpdateCheck = Date.now();
|
|
279
|
+
checkingUpdate = hasNewVersion()
|
|
280
|
+
.then((found) => {
|
|
281
|
+
if (found) updateAvailable.value = true;
|
|
282
|
+
return found;
|
|
283
|
+
})
|
|
284
|
+
.finally(() => {
|
|
285
|
+
checkingUpdate = null;
|
|
286
|
+
});
|
|
287
|
+
return checkingUpdate;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** 「立即重试」:跳过退避马上重连。 */
|
|
291
|
+
function retryNow(): void {
|
|
292
|
+
ws?.retryNow();
|
|
293
|
+
}
|
|
294
|
+
|
|
230
295
|
/** 默认最多等一个调用超时(connect 时配置,默认 10 秒)。 */
|
|
231
296
|
async function waitUntilConnected(timeoutMs?: number): Promise<void> {
|
|
232
297
|
if (connected.value) return;
|
|
@@ -429,6 +494,11 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
429
494
|
accessToken,
|
|
430
495
|
refreshToken,
|
|
431
496
|
connected,
|
|
497
|
+
disconnectedAt,
|
|
498
|
+
restarting,
|
|
499
|
+
updateAvailable,
|
|
500
|
+
checkUpdate,
|
|
501
|
+
retryNow,
|
|
432
502
|
isLoggedIn,
|
|
433
503
|
sessionMessage,
|
|
434
504
|
tables,
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 前端版本检测与安全重载。
|
|
3
|
+
*
|
|
4
|
+
* 不需要后端配合:当前页面的入口脚本地址(构建产物带哈希,如 /assets/index-abc.js)就是版本。
|
|
5
|
+
* 取一次最新的首页 HTML(不走缓存),入口脚本地址不同就说明重新部署过前端。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const ENTRY_SCRIPT =
|
|
9
|
+
/<script[^>]+type=["']module["'][^>]*\ssrc=["']([^"']+)["']/i;
|
|
10
|
+
const RELOAD_GUARD_KEY = "ys_reload_guard_at";
|
|
11
|
+
const RELOAD_MESSAGE_KEY = "ys_reload_message";
|
|
12
|
+
/** 这段时间内已经自动重载过,就不再自动重载,避免服务端异常时反复刷新。 */
|
|
13
|
+
const RELOAD_GUARD_MS = 20_000;
|
|
14
|
+
|
|
15
|
+
/** 当前页面运行的入口脚本地址;开发模式(/src/main.ts)同样适用,前后一致即视为未更新。 */
|
|
16
|
+
export function currentEntry(): string {
|
|
17
|
+
if (typeof document === "undefined") return "";
|
|
18
|
+
const script = document.querySelector<HTMLScriptElement>(
|
|
19
|
+
'script[type="module"][src]',
|
|
20
|
+
);
|
|
21
|
+
return script?.getAttribute("src") ?? "";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 读取服务端最新首页里的入口脚本地址。服务不可用或不是 HTML 时返回空串。 */
|
|
25
|
+
export async function latestEntry(): Promise<string> {
|
|
26
|
+
if (typeof fetch === "undefined" || typeof location === "undefined")
|
|
27
|
+
return "";
|
|
28
|
+
try {
|
|
29
|
+
const url = new URL(location.pathname || "/", location.origin);
|
|
30
|
+
url.searchParams.set("_ys_v", String(Date.now()));
|
|
31
|
+
const response = await fetch(url, {
|
|
32
|
+
cache: "no-store",
|
|
33
|
+
credentials: "same-origin",
|
|
34
|
+
});
|
|
35
|
+
if (
|
|
36
|
+
!response.ok ||
|
|
37
|
+
!(response.headers.get("content-type") ?? "").includes("html")
|
|
38
|
+
)
|
|
39
|
+
return "";
|
|
40
|
+
return ENTRY_SCRIPT.exec(await response.text())?.[1] ?? "";
|
|
41
|
+
} catch {
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 服务端已有新版本前端时返回 true。 */
|
|
47
|
+
export async function hasNewVersion(): Promise<boolean> {
|
|
48
|
+
const current = currentEntry();
|
|
49
|
+
if (!current) return false;
|
|
50
|
+
const latest = await latestEntry();
|
|
51
|
+
return !!latest && latest !== current;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 页面按需加载的代码文件已不存在(重新部署后文件名变了)或加载失败。 */
|
|
55
|
+
export function isStaleChunkError(error: unknown): boolean {
|
|
56
|
+
const text = String((error as { message?: unknown })?.message ?? error ?? "");
|
|
57
|
+
return /Failed to fetch dynamically imported module|error loading dynamically imported module|Importing a module script failed|Unable to preload CSS|Loading chunk \S+ failed|ChunkLoadError/i.test(
|
|
58
|
+
text,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 重新加载页面以使用最新版本前端。hash 给出时先切到该路由(hash 路由系统用 to.fullPath)。
|
|
64
|
+
* message 会在重新加载后由 takeReloadMessage() 取出提示一次。
|
|
65
|
+
* 返回 false 表示短时间内已经自动重载过,这次不再重载(交给页面提示用户手动刷新)。
|
|
66
|
+
*/
|
|
67
|
+
export function reloadPage(
|
|
68
|
+
options: { hash?: string; message?: string; force?: boolean } = {},
|
|
69
|
+
): boolean {
|
|
70
|
+
if (typeof location === "undefined") return false;
|
|
71
|
+
const now = Date.now();
|
|
72
|
+
const last = Number(sessionStorage.getItem(RELOAD_GUARD_KEY) || 0);
|
|
73
|
+
if (!options.force && now - last < RELOAD_GUARD_MS) return false;
|
|
74
|
+
sessionStorage.setItem(RELOAD_GUARD_KEY, String(now));
|
|
75
|
+
if (options.message)
|
|
76
|
+
sessionStorage.setItem(RELOAD_MESSAGE_KEY, options.message);
|
|
77
|
+
if (options.hash)
|
|
78
|
+
history.replaceState(
|
|
79
|
+
history.state,
|
|
80
|
+
"",
|
|
81
|
+
`#${options.hash.replace(/^#/, "")}`,
|
|
82
|
+
);
|
|
83
|
+
location.reload();
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 取出并清除重新加载前留下的提示语。 */
|
|
88
|
+
export function takeReloadMessage(): string {
|
|
89
|
+
if (typeof sessionStorage === "undefined") return "";
|
|
90
|
+
const message = sessionStorage.getItem(RELOAD_MESSAGE_KEY) ?? "";
|
|
91
|
+
sessionStorage.removeItem(RELOAD_MESSAGE_KEY);
|
|
92
|
+
return message;
|
|
93
|
+
}
|
package/src/ws.ts
CHANGED
|
@@ -30,6 +30,28 @@ export class YsRpcError extends Error {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** 请求还没发出,连接就一直没恢复:服务端没收到,可以放心重试。 */
|
|
34
|
+
export class YsOfflineError extends Error {
|
|
35
|
+
constructor(message = "服务暂时不可用,请稍后再试") {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "YsOfflineError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 请求已经发出但连接断了:服务端可能已经执行,结果未知,不要自动重试写操作。 */
|
|
42
|
+
export class YsConnectionLostError extends Error {
|
|
43
|
+
constructor(message = "连接中断,操作结果未知,请刷新数据后核对") {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "YsConnectionLostError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 断开事件附带的信息。restarting 表示服务端主动重启(关闭码 1012 / 1001),多半是在部署新版本。 */
|
|
50
|
+
export interface YsDisconnectInfo {
|
|
51
|
+
code: number;
|
|
52
|
+
restarting: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
33
55
|
type EventHandler = (data: unknown) => void;
|
|
34
56
|
|
|
35
57
|
/** 回到前台、空闲多久没收到任何消息就探测一次。 */
|
|
@@ -39,6 +61,12 @@ const HEARTBEAT_TICK_MS = 10_000;
|
|
|
39
61
|
/** 探测超时。超时即判定连接假死,立即重连。 */
|
|
40
62
|
const PROBE_TIMEOUT_MS = 3_000;
|
|
41
63
|
const RECONNECT_MAX_MS = 30_000;
|
|
64
|
+
/** 服务端重启(部署)期间按这个间隔快速重连,恢复后第一时间连上。 */
|
|
65
|
+
const RESTART_RECONNECT_MS = 1_500;
|
|
66
|
+
/** 服务端「服务重启」「正在离开」的关闭码。 */
|
|
67
|
+
const RESTART_CLOSE_CODES = new Set([1001, 1012]);
|
|
68
|
+
/** 断线时,还没发出的请求最多等连接恢复这么久(默认 2 分钟),覆盖一次部署的停机。 */
|
|
69
|
+
export const DEFAULT_OFFLINE_WAIT_MS = 120_000;
|
|
42
70
|
/** 调用超时:默认 10 秒,可整体配置,也可单次调用覆盖。超出范围的值收到 1 秒~10 分钟之间。 */
|
|
43
71
|
export const DEFAULT_CALL_TIMEOUT_MS = 10_000;
|
|
44
72
|
export const MIN_CALL_TIMEOUT_MS = 1_000;
|
|
@@ -47,6 +75,8 @@ export const MAX_CALL_TIMEOUT_MS = 600_000;
|
|
|
47
75
|
export interface YsWsClientOptions {
|
|
48
76
|
/** 本连接所有调用的默认超时(毫秒)。不传用 10 秒。 */
|
|
49
77
|
callTimeoutMs?: number;
|
|
78
|
+
/** 断线时还没发出的请求最多等多久(毫秒)。不传用 2 分钟。 */
|
|
79
|
+
offlineWaitMs?: number;
|
|
50
80
|
}
|
|
51
81
|
|
|
52
82
|
/** 没给、不是正数时用 fallback;其余收到允许范围内。 */
|
|
@@ -86,6 +116,9 @@ export class YsWsClient {
|
|
|
86
116
|
};
|
|
87
117
|
|
|
88
118
|
private readonly callTimeout: number;
|
|
119
|
+
private readonly offlineWait: number;
|
|
120
|
+
/** 最近一次断开是否是服务端主动重启,连上 hello 后清除。 */
|
|
121
|
+
private restarting = false;
|
|
89
122
|
|
|
90
123
|
/** 第二个参数可传 `{ callTimeoutMs }`;为兼容旧写法也可直接传毫秒数。 */
|
|
91
124
|
constructor(
|
|
@@ -94,6 +127,10 @@ export class YsWsClient {
|
|
|
94
127
|
) {
|
|
95
128
|
const raw = typeof options === "number" ? options : options.callTimeoutMs;
|
|
96
129
|
this.callTimeout = normalizeTimeout(raw, DEFAULT_CALL_TIMEOUT_MS);
|
|
130
|
+
this.offlineWait = normalizeTimeout(
|
|
131
|
+
typeof options === "number" ? undefined : options.offlineWaitMs,
|
|
132
|
+
DEFAULT_OFFLINE_WAIT_MS,
|
|
133
|
+
);
|
|
97
134
|
}
|
|
98
135
|
|
|
99
136
|
get connected(): boolean {
|
|
@@ -105,6 +142,21 @@ export class YsWsClient {
|
|
|
105
142
|
return this.callTimeout;
|
|
106
143
|
}
|
|
107
144
|
|
|
145
|
+
/** 断线时还没发出的请求最多等多久(毫秒)。 */
|
|
146
|
+
get offlineWaitMs(): number {
|
|
147
|
+
return this.offlineWait;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 立即重连(跳过退避),用于「立即重试」按钮。 */
|
|
151
|
+
retryNow(): void {
|
|
152
|
+
if (this.stopped || this.ready) return;
|
|
153
|
+
if (this.ws?.readyState === WebSocket.CONNECTING) return;
|
|
154
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
155
|
+
this.reconnectTimer = null;
|
|
156
|
+
this.reconnectDelay = 1000;
|
|
157
|
+
this.connect();
|
|
158
|
+
}
|
|
159
|
+
|
|
108
160
|
start(): void {
|
|
109
161
|
this.stopped = false;
|
|
110
162
|
this.connect();
|
|
@@ -175,15 +227,12 @@ export class YsWsClient {
|
|
|
175
227
|
timeoutMs?: number,
|
|
176
228
|
): Promise<unknown> {
|
|
177
229
|
const limit = normalizeTimeout(timeoutMs, this.callTimeout);
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
) {
|
|
182
|
-
throw new Error("not connected");
|
|
230
|
+
// 还没发出的请求,服务端肯定没收到,断线时就等连接恢复再发(最多 offlineWaitMs),部署期间用户无感。
|
|
231
|
+
if (!this.ready && !(await this.waitReady(this.offlineWait))) {
|
|
232
|
+
throw new YsOfflineError();
|
|
183
233
|
}
|
|
184
234
|
const ws = this.ws;
|
|
185
|
-
if (!ws || ws.readyState !== WebSocket.OPEN)
|
|
186
|
-
throw new Error("not connected");
|
|
235
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) throw new YsOfflineError();
|
|
187
236
|
const id = `${this.connectionId || "c0"}:${++this.seq}`;
|
|
188
237
|
ws.send(JSON.stringify({ event, id, ts: Date.now() / 1000, data }));
|
|
189
238
|
return new Promise((resolve, reject) => {
|
|
@@ -278,13 +327,15 @@ export class YsWsClient {
|
|
|
278
327
|
if (!this.stopped) this.connect();
|
|
279
328
|
}
|
|
280
329
|
|
|
281
|
-
private handleClosed(): void {
|
|
330
|
+
private handleClosed(code = 0): void {
|
|
282
331
|
for (const p of this.pending.values())
|
|
283
|
-
p.reject(new
|
|
332
|
+
p.reject(new YsConnectionLostError());
|
|
284
333
|
this.pending.clear();
|
|
285
334
|
const wasReady = this.ready;
|
|
286
335
|
this.ready = false;
|
|
287
|
-
if (
|
|
336
|
+
if (RESTART_CLOSE_CODES.has(code)) this.restarting = true;
|
|
337
|
+
const info: YsDisconnectInfo = { code, restarting: this.restarting };
|
|
338
|
+
if (wasReady || this.ws) this.emit("__disconnected", info);
|
|
288
339
|
}
|
|
289
340
|
|
|
290
341
|
private connect(): void {
|
|
@@ -294,17 +345,18 @@ export class YsWsClient {
|
|
|
294
345
|
this.reconnectDelay = 1000;
|
|
295
346
|
};
|
|
296
347
|
ws.onmessage = (e) => this.onMessage(e);
|
|
297
|
-
ws.onclose = () => {
|
|
348
|
+
ws.onclose = (event?: CloseEvent) => {
|
|
298
349
|
if (ws !== this.ws) return;
|
|
299
|
-
this.handleClosed();
|
|
350
|
+
this.handleClosed(event?.code ?? 0);
|
|
300
351
|
if (!this.stopped) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
this.
|
|
307
|
-
|
|
352
|
+
// 服务端在重启(部署)时按固定短间隔重连,不指数退避,恢复后第一时间连上。
|
|
353
|
+
const delay = this.restarting
|
|
354
|
+
? RESTART_RECONNECT_MS
|
|
355
|
+
: this.reconnectDelay + Math.random() * 1000;
|
|
356
|
+
this.reconnectTimer = setTimeout(() => {
|
|
357
|
+
this.reconnectTimer = null;
|
|
358
|
+
this.connect();
|
|
359
|
+
}, delay);
|
|
308
360
|
this.reconnectDelay = Math.min(
|
|
309
361
|
this.reconnectDelay * 2,
|
|
310
362
|
RECONNECT_MAX_MS,
|
|
@@ -343,6 +395,7 @@ export class YsWsClient {
|
|
|
343
395
|
);
|
|
344
396
|
applyPasswordWrap(msg.data);
|
|
345
397
|
this.ready = true;
|
|
398
|
+
this.restarting = false;
|
|
346
399
|
const waiters = this.readyWaiters;
|
|
347
400
|
this.readyWaiters = [];
|
|
348
401
|
for (const resolve of waiters) resolve();
|