@colyseus/sdk 0.17.42 → 0.17.43

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.
Files changed (48) hide show
  1. package/build/3rd_party/discord.cjs +1 -1
  2. package/build/3rd_party/discord.mjs +1 -1
  3. package/build/Auth.cjs +1 -1
  4. package/build/Auth.mjs +1 -1
  5. package/build/Client.cjs +39 -10
  6. package/build/Client.cjs.map +1 -1
  7. package/build/Client.d.ts +11 -2
  8. package/build/Client.mjs +38 -10
  9. package/build/Client.mjs.map +1 -1
  10. package/build/Connection.cjs +1 -1
  11. package/build/Connection.mjs +1 -1
  12. package/build/HTTP.cjs +1 -1
  13. package/build/HTTP.mjs +1 -1
  14. package/build/Room.cjs +1 -1
  15. package/build/Room.mjs +1 -1
  16. package/build/Storage.cjs +1 -1
  17. package/build/Storage.mjs +1 -1
  18. package/build/core/nanoevents.cjs +1 -1
  19. package/build/core/nanoevents.mjs +1 -1
  20. package/build/core/signal.cjs +1 -1
  21. package/build/core/signal.mjs +1 -1
  22. package/build/core/utils.cjs +1 -1
  23. package/build/core/utils.mjs +1 -1
  24. package/build/debug.cjs +1 -1
  25. package/build/debug.mjs +1 -1
  26. package/build/errors/Errors.cjs +1 -1
  27. package/build/errors/Errors.mjs +1 -1
  28. package/build/fetchXHR.cjs +1 -1
  29. package/build/fetchXHR.mjs +1 -1
  30. package/build/index.cjs +1 -1
  31. package/build/index.mjs +1 -1
  32. package/build/legacy.cjs +1 -1
  33. package/build/legacy.mjs +1 -1
  34. package/build/serializer/NoneSerializer.cjs +1 -1
  35. package/build/serializer/NoneSerializer.mjs +1 -1
  36. package/build/serializer/SchemaSerializer.cjs +1 -1
  37. package/build/serializer/SchemaSerializer.mjs +1 -1
  38. package/build/serializer/Serializer.cjs +1 -1
  39. package/build/serializer/Serializer.mjs +1 -1
  40. package/build/transport/H3Transport.cjs +1 -1
  41. package/build/transport/H3Transport.mjs +1 -1
  42. package/build/transport/WebSocketTransport.cjs +1 -1
  43. package/build/transport/WebSocketTransport.mjs +1 -1
  44. package/dist/colyseus.js +39 -10
  45. package/dist/colyseus.js.map +1 -1
  46. package/dist/debug.js +1 -1
  47. package/package.json +2 -2
  48. package/src/Client.ts +41 -8
package/dist/debug.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // This software is released under the MIT License.
4
4
  // https://opensource.org/license/MIT
5
5
  //
6
- // colyseus.js@0.17.42
6
+ // colyseus.js@0.17.43
7
7
  (function (Client_ts, sharedTypes) {
8
8
  'use strict';
9
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/sdk",
3
- "version": "0.17.42",
3
+ "version": "0.17.43",
4
4
  "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
5
5
  "author": "Endel Dreyer",
6
6
  "license": "MIT",
@@ -82,7 +82,7 @@
82
82
  "typescript": "^5.9.3",
83
83
  "vite": "^5.0.11",
84
84
  "vitest": "^2.1.1",
85
- "@colyseus/core": "^0.17.42"
85
+ "@colyseus/core": "^0.17.43"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "@colyseus/core": "0.17.x"
package/src/Client.ts CHANGED
@@ -38,6 +38,11 @@ export interface LatencyOptions {
38
38
  protocol?: "ws" | "h3";
39
39
  /** Number of pings to send (default: 1). Returns the average latency when > 1. */
40
40
  pingCount?: number;
41
+ /**
42
+ * Milliseconds to wait for the measurement before rejecting (default: 1500).
43
+ * Bounds unreachable/blackholed endpoints so they can't stall selection.
44
+ */
45
+ timeout?: number;
41
46
  }
42
47
 
43
48
  export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
@@ -131,7 +136,7 @@ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
131
136
  * Select the endpoint with the lowest latency.
132
137
  * @param endpoints Array of endpoints to select from.
133
138
  * @param options Client options.
134
- * @param latencyOptions Latency measurement options (protocol, pingCount).
139
+ * @param latencyOptions Latency measurement options (protocol, pingCount, timeout) — forwarded to each {@link getLatency} call.
135
140
  * @returns The client with the lowest latency.
136
141
  */
137
142
  static async selectByLatency<ServerType extends SDKTypes = any, UserData = any>(
@@ -313,16 +318,39 @@ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
313
318
 
314
319
  /**
315
320
  * Create a new connection with the server, and measure the latency.
316
- * @param options Latency measurement options (protocol, pingCount).
321
+ *
322
+ * Always settles: resolves with the (average) round-trip time, or rejects on
323
+ * connection error, server-side close before all pongs arrive, or timeout.
324
+ *
325
+ * @param options Latency measurement options (protocol, pingCount, timeout).
317
326
  */
318
327
  public getLatency(options: LatencyOptions = {}): Promise<number> {
319
328
  const protocol = options.protocol ?? "ws";
320
329
  const pingCount = options.pingCount ?? 1;
330
+ const timeout = options.timeout ?? 1500;
321
331
 
322
332
  return new Promise<number>((resolve, reject) => {
323
333
  const conn = new Connection(protocol);
324
334
  const latencies: number[] = [];
325
335
  let pingStart = 0;
336
+ let settled = false;
337
+ let timeoutId: ReturnType<typeof setTimeout>;
338
+
339
+ // run exactly once — guards against late events after resolve/reject
340
+ // (e.g. our own conn.close() firing onclose, or a stray onclose/onerror pair)
341
+ const settle = (run: () => void) => {
342
+ if (settled) { return; }
343
+ settled = true;
344
+ clearTimeout(timeoutId);
345
+ try { conn.close(); } catch (e) { /* socket may never have opened */ }
346
+ run();
347
+ };
348
+
349
+ const fail = (message: string) =>
350
+ settle(() => reject(new ServerError(CloseCode.ABNORMAL_CLOSURE, `Failed to get latency: ${message}`)));
351
+
352
+ // bound blackholed/filtered hosts that never fire onopen/onerror within the OS TCP timeout
353
+ timeoutId = setTimeout(() => fail(`timed out after ${timeout}ms`), timeout);
326
354
 
327
355
  conn.events.onopen = () => {
328
356
  pingStart = Date.now();
@@ -338,17 +366,22 @@ export class ColyseusSDK<ServerType extends SDKTypes = any, UserData = any> {
338
366
  conn.send(new Uint8Array([Protocol.PING]));
339
367
  } else {
340
368
  // Done, calculate average and close
341
- conn.close();
342
369
  const average = latencies.reduce((sum, l) => sum + l, 0) / latencies.length;
343
- resolve(average);
370
+ settle(() => resolve(average));
344
371
  }
345
372
  };
346
373
 
347
- conn.events.onerror = (event: ErrorEvent) => {
348
- reject(new ServerError(CloseCode.ABNORMAL_CLOSURE, `Failed to get latency: ${event.message}`));
349
- };
374
+ // server closed the socket before all pongs arrived — fires without onerror on a clean close
375
+ conn.events.onclose = (event: any) =>
376
+ fail(`connection closed${event?.code ? ` (${event.code})` : ""}${event?.reason ? `: ${event.reason}` : ""}`);
350
377
 
351
- conn.connect(this.getHttpEndpoint());
378
+ conn.events.onerror = (event: ErrorEvent) => fail(event.message);
379
+
380
+ try {
381
+ conn.connect(this.getHttpEndpoint());
382
+ } catch (e: any) {
383
+ fail(e?.message ?? "failed to connect");
384
+ }
352
385
  });
353
386
  }
354
387