castle-web-sdk 0.4.49 → 0.4.50

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/README.md CHANGED
@@ -1372,6 +1372,12 @@ destination session's content pack hash) and `contentUrl` (that pack's immutable
1372
1372
  URL, or null when the destination has none), or `cancelled`, `full`,
1373
1373
  `unavailable`, `not_found`, `forbidden` or `party_required` with those fields null.
1374
1374
 
1375
+ Castle lists a player at a door only once it has seen them in the place's
1376
+ session, which can take up to 15 seconds after they arrive or after their
1377
+ connection comes back from a break (an app switch, a network drop). In that
1378
+ window `go` and `jump` keep asking and answer once the player is listed, so a
1379
+ `forbidden` they return is final.
1380
+
1375
1381
  A door can also lead to a cabinet: a classic Castle deck the host plays itself.
1376
1382
  `Hub.go` then answers `status: "ok"` with `kind: "classic"` and null `url`, `nonce`
1377
1383
  and `sessionId`: there is no session to join, and the trip is already committed.
package/dist/hub.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { CastleError } from "./errors";
2
2
  import { hostRequest, setHubEventHandler } from "./transport";
3
+ import { HUB_FORBIDDEN_RETRY_MS, rejoinedRecently } from "./hubRejoin";
3
4
  export const Hub = {
4
5
  go,
5
6
  back,
@@ -36,7 +37,22 @@ async function go(doorId, options = {}) {
36
37
  operation: "Hub.go",
37
38
  });
38
39
  }
39
- return hostRequest("hub.go", ownerUserId === undefined ? { doorId } : { doorId, ownerUserId });
40
+ return untilListed(() => hostRequest("hub.go", ownerUserId === undefined ? { doorId } : { doorId, ownerUserId }));
41
+ }
42
+ // Right after the socket rejoined its session ghost may not list the player
43
+ // yet, and answers `forbidden` until it does. `Hub.cancel` ends the wait.
44
+ let cancels = 0;
45
+ async function untilListed(ask) {
46
+ const asked = cancels;
47
+ for (;;) {
48
+ const result = await ask();
49
+ if (result.status !== "forbidden" || !rejoinedRecently())
50
+ return result;
51
+ await new Promise((resolve) => setTimeout(resolve, HUB_FORBIDDEN_RETRY_MS));
52
+ if (cancels !== asked) {
53
+ return { status: "cancelled", url: null, nonce: null, sessionId: null, bundleHash: null, contentUrl: null };
54
+ }
55
+ }
40
56
  }
41
57
  async function back() {
42
58
  return hostRequest("hub.back", {});
@@ -99,7 +115,7 @@ async function jump(userId) {
99
115
  operation: "Hub.jump",
100
116
  });
101
117
  }
102
- return hostRequest("hub.jump", { userId });
118
+ return untilListed(() => hostRequest("hub.jump", { userId }));
103
119
  }
104
120
  async function report(userId, reason) {
105
121
  if (typeof userId !== "string" || userId.length === 0) {
@@ -135,6 +151,7 @@ async function reportChat(messageId, options) {
135
151
  return hostRequest("hub.reportChat", { messageId, ...(options?.block === true ? { block: true } : {}) });
136
152
  }
137
153
  async function cancel() {
154
+ cancels += 1;
138
155
  return hostRequest("hub.cancel", {});
139
156
  }
140
157
  const backListeners = new Set();
@@ -0,0 +1,4 @@
1
+ export declare const HUB_ROSTER_LAG_MS = 16000;
2
+ export declare const HUB_FORBIDDEN_RETRY_MS = 1000;
3
+ export declare function noteSessionRejoined(): void;
4
+ export declare function rejoinedRecently(): boolean;
@@ -0,0 +1,15 @@
1
+ // Ghost's source check for a door reads the session's heartbeat roster, which
2
+ // drops a player who was away and takes up to 15 s to list them again. The host
3
+ // waits that out after an arrival it saw; a socket that reconnected to the same
4
+ // session is one only the deck sees, so the deck waits it out itself.
5
+ export const HUB_ROSTER_LAG_MS = 16_000;
6
+ export const HUB_FORBIDDEN_RETRY_MS = 1_000;
7
+ let rejoinedAt = null;
8
+ // Monotonic where there is one: a wall-clock correction must not stretch or cut the wait.
9
+ const now = () => globalThis.performance?.now() ?? Date.now();
10
+ export function noteSessionRejoined() {
11
+ rejoinedAt = now();
12
+ }
13
+ export function rejoinedRecently() {
14
+ return rejoinedAt !== null && now() - rejoinedAt < HUB_ROSTER_LAG_MS;
15
+ }
@@ -3,6 +3,7 @@ import { BINARY_MESSAGE_TAG, isBinaryPayload, MAX_CLIENT_FRAME_BYTES, toUint8Arr
3
3
  import { hostRequest } from "./transport";
4
4
  import { MultiplayerVoiceImpl } from "./multiplayerVoice";
5
5
  import { ReconnectBudget } from "./reconnectBudget";
6
+ import { noteSessionRejoined } from "./hubRejoin";
6
7
  const KEEPALIVE_INTERVAL_MS = 25_000;
7
8
  const HELLO_TIMEOUT_MS = 10_000;
8
9
  const RECONNECT_BUDGET_MS = 30_000;
@@ -205,6 +206,7 @@ class MultiplayerConnectionImpl {
205
206
  pongExtensionUsed = false;
206
207
  pongDeadline = null;
207
208
  reAcquisitionUsed = false;
209
+ greeted = false;
208
210
  recovering = false;
209
211
  reconnectBudget = null;
210
212
  hubSettled = false;
@@ -580,6 +582,11 @@ class MultiplayerConnectionImpl {
580
582
  });
581
583
  }
582
584
  acceptHello(frame) {
585
+ // A later hello is a rejoin the host never hears of; doors wait out the
586
+ // roster lag after it (hubRejoin.ts).
587
+ if (this.greeted)
588
+ noteSessionRejoined();
589
+ this.greeted = true;
583
590
  this.currentPlayerId = frame.playerId;
584
591
  this.currentSessionId = frame.sessionId;
585
592
  // Diagnostics never decide whether a hello is accepted. Missing or malformed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-sdk",
3
- "version": "0.4.49",
3
+ "version": "0.4.50",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"