@jskit-ai/realtime 0.1.247 → 0.1.248
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/realtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.248",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"socket.io-client": "^4.8.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@jskit-ai/kernel": "0.1.
|
|
23
|
+
"@jskit-ai/kernel": "0.1.248",
|
|
24
24
|
"vue": "^3.5.13"
|
|
25
25
|
},
|
|
26
26
|
"description": "Thin, generic realtime runtime wrappers for socket.io server and client.",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
"subpath": "./client/runtime",
|
|
93
|
-
"summary": "Exports createSocketIoClient and
|
|
93
|
+
"summary": "Exports createSocketIoClient, disconnectSocketIoClient and reconnectSocketIoClient."
|
|
94
94
|
}
|
|
95
95
|
],
|
|
96
96
|
"capabilities": {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { computed, defineComponent, h, onBeforeUnmount, onMounted, ref } from "vue";
|
|
2
2
|
import { EMPTY_REALTIME_SOCKET, useRealtimeSocket } from "../composables/useRealtimeEvent.js";
|
|
3
|
+
import { reconnectSocketIoClient } from "../runtime.js";
|
|
3
4
|
|
|
4
5
|
const ROOT_STYLE = Object.freeze({
|
|
5
6
|
alignItems: "center",
|
|
@@ -111,7 +112,7 @@ const RealtimeConnectionIndicator = defineComponent({
|
|
|
111
112
|
"button",
|
|
112
113
|
{
|
|
113
114
|
disabled: !realtimeAvailable || connected.value,
|
|
114
|
-
onClick: () => socket
|
|
115
|
+
onClick: () => reconnectSocketIoClient(socket),
|
|
115
116
|
style: { ...ROOT_STYLE, cursor: realtimeAvailable && !connected.value ? "pointer" : "default" },
|
|
116
117
|
title: tooltipText.value,
|
|
117
118
|
type: "button",
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { reconnectSocketIoClient } from "./runtime.js";
|
|
2
|
+
|
|
1
3
|
function attachSocketConnectionRecovery(socket) {
|
|
2
4
|
let timer = null;
|
|
3
5
|
let delay = 1_000;
|
|
@@ -44,9 +46,12 @@ function attachSocketConnectionRecovery(socket) {
|
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
function onAvailable() {
|
|
47
|
-
if (
|
|
49
|
+
if (socket.connected || document?.hidden || window?.navigator?.onLine === false ||
|
|
50
|
+
!socket.io.reconnection() || (!socket.active && !recovering)) return;
|
|
51
|
+
// A page that wakes or comes back online reconnects now, not after the
|
|
52
|
+
// remaining backoff delay that was scheduled before it slept.
|
|
48
53
|
clearRetry();
|
|
49
|
-
socket
|
|
54
|
+
reconnectSocketIoClient(socket);
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
socket.on("connect", onConnect);
|
package/src/client/runtime.js
CHANGED
|
@@ -31,7 +31,21 @@ function disconnectSocketIoClient(socket) {
|
|
|
31
31
|
socket.disconnect();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// Socket.IO ignores connect() while its own reconnection waits out a backoff
|
|
35
|
+
// delay or an attempt left hanging by a sleeping laptop. Closing that pending
|
|
36
|
+
// attempt first makes connect() open a fresh transport immediately.
|
|
37
|
+
function reconnectSocketIoClient(socket) {
|
|
38
|
+
if (!socket || socket.connected || typeof socket.connect !== "function") {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (socket.active) {
|
|
42
|
+
socket.disconnect();
|
|
43
|
+
}
|
|
44
|
+
socket.connect();
|
|
45
|
+
}
|
|
46
|
+
|
|
34
47
|
export {
|
|
35
48
|
createSocketIoClient,
|
|
36
|
-
disconnectSocketIoClient
|
|
49
|
+
disconnectSocketIoClient,
|
|
50
|
+
reconnectSocketIoClient
|
|
37
51
|
};
|
|
@@ -4,7 +4,7 @@ import { EventEmitter, once } from "node:events";
|
|
|
4
4
|
import { createServer } from "node:http";
|
|
5
5
|
import { Server } from "socket.io";
|
|
6
6
|
|
|
7
|
-
import { createSocketIoClient, disconnectSocketIoClient } from "../src/client/runtime.js";
|
|
7
|
+
import { createSocketIoClient, disconnectSocketIoClient, reconnectSocketIoClient } from "../src/client/runtime.js";
|
|
8
8
|
import { attachSocketConnectionRecovery } from "../src/client/connectionRecovery.js";
|
|
9
9
|
|
|
10
10
|
test("createSocketIoClient calls connect with fixed socket path", () => {
|
|
@@ -212,3 +212,50 @@ test("a real server-forced disconnect reconnects through a fresh authenticated h
|
|
|
212
212
|
assert.equal(handshakes, 3);
|
|
213
213
|
assert.notEqual(client.id, before);
|
|
214
214
|
});
|
|
215
|
+
|
|
216
|
+
test("reconnect now opens a fresh transport while Socket.IO waits out its backoff delay", async (t) => {
|
|
217
|
+
const http = createServer();
|
|
218
|
+
const io = new Server(http);
|
|
219
|
+
http.listen(0, "127.0.0.1");
|
|
220
|
+
await once(http, "listening");
|
|
221
|
+
const client = createSocketIoClient({
|
|
222
|
+
url: `http://127.0.0.1:${http.address().port}`,
|
|
223
|
+
options: { randomizationFactor: 0, reconnectionDelay: 60_000, reconnectionDelayMax: 60_000 }
|
|
224
|
+
});
|
|
225
|
+
t.after(async () => {
|
|
226
|
+
client.disconnect();
|
|
227
|
+
await new Promise((resolve) => io.close(resolve));
|
|
228
|
+
});
|
|
229
|
+
await once(client, "connect", { signal: AbortSignal.timeout(5_000) });
|
|
230
|
+
const disconnected = once(client, "disconnect");
|
|
231
|
+
io.sockets.sockets.get(client.id).conn.close();
|
|
232
|
+
assert.equal((await disconnected)[0], "transport close");
|
|
233
|
+
assert.equal(client.active, true);
|
|
234
|
+
client.connect();
|
|
235
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
236
|
+
assert.equal(client.connected, false, "plain connect() waits for the 60 s backoff");
|
|
237
|
+
const connected = once(client, "connect", { signal: AbortSignal.timeout(5_000) });
|
|
238
|
+
reconnectSocketIoClient(client);
|
|
239
|
+
await connected;
|
|
240
|
+
assert.equal(client.connected, true);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("a page that wakes mid-backoff reconnects now unless the app disconnected on purpose", (t) => {
|
|
244
|
+
const previousDocument = globalThis.document;
|
|
245
|
+
const document = Object.assign(new EventTarget(), { hidden: false });
|
|
246
|
+
globalThis.document = document;
|
|
247
|
+
t.after(() => {
|
|
248
|
+
if (previousDocument === undefined) delete globalThis.document;
|
|
249
|
+
else globalThis.document = previousDocument;
|
|
250
|
+
});
|
|
251
|
+
const { socket } = recoverySocket(t);
|
|
252
|
+
socket.disconnect = t.mock.fn(() => { socket.active = false; });
|
|
253
|
+
socket.active = true;
|
|
254
|
+
socket.emit("disconnect", "transport close");
|
|
255
|
+
document.dispatchEvent(new Event("visibilitychange"));
|
|
256
|
+
assert.equal(socket.disconnect.mock.callCount(), 1);
|
|
257
|
+
assert.equal(socket.connect.mock.callCount(), 1);
|
|
258
|
+
socket.emit("disconnect", "io client disconnect");
|
|
259
|
+
document.dispatchEvent(new Event("visibilitychange"));
|
|
260
|
+
assert.equal(socket.connect.mock.callCount(), 1);
|
|
261
|
+
});
|