@colyseus/sdk 0.17.13 → 0.17.15

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 (72) 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 +3 -2
  6. package/build/Client.cjs.map +1 -1
  7. package/build/Client.d.ts +1 -1
  8. package/build/Client.mjs +3 -2
  9. package/build/Client.mjs.map +1 -1
  10. package/build/Connection.cjs +30 -1
  11. package/build/Connection.cjs.map +1 -1
  12. package/build/Connection.d.ts +1 -0
  13. package/build/Connection.mjs +27 -1
  14. package/build/Connection.mjs.map +1 -1
  15. package/build/HTTP.cjs +1 -1
  16. package/build/HTTP.mjs +1 -1
  17. package/build/Protocol.cjs +1 -1
  18. package/build/Protocol.mjs +1 -1
  19. package/build/Room.cjs +17 -14
  20. package/build/Room.cjs.map +1 -1
  21. package/build/Room.mjs +12 -9
  22. package/build/Room.mjs.map +1 -1
  23. package/build/Storage.cjs +1 -1
  24. package/build/Storage.mjs +1 -1
  25. package/build/core/nanoevents.cjs +1 -1
  26. package/build/core/nanoevents.mjs +1 -1
  27. package/build/core/signal.cjs +1 -1
  28. package/build/core/signal.mjs +1 -1
  29. package/build/core/utils.cjs +1 -1
  30. package/build/core/utils.mjs +1 -1
  31. package/build/debug.cjs +226 -113
  32. package/build/debug.cjs.map +1 -1
  33. package/build/debug.mjs +226 -113
  34. package/build/debug.mjs.map +1 -1
  35. package/build/errors/Errors.cjs +1 -12
  36. package/build/errors/Errors.cjs.map +1 -1
  37. package/build/errors/Errors.d.ts +0 -10
  38. package/build/errors/Errors.mjs +2 -13
  39. package/build/errors/Errors.mjs.map +1 -1
  40. package/build/index.cjs +11 -11
  41. package/build/index.cjs.map +1 -1
  42. package/build/index.d.ts +2 -2
  43. package/build/index.mjs +3 -3
  44. package/build/index.mjs.map +1 -1
  45. package/build/legacy.cjs +1 -1
  46. package/build/legacy.mjs +1 -1
  47. package/build/serializer/NoneSerializer.cjs +1 -1
  48. package/build/serializer/NoneSerializer.mjs +1 -1
  49. package/build/serializer/SchemaSerializer.cjs +1 -1
  50. package/build/serializer/SchemaSerializer.mjs +1 -1
  51. package/build/serializer/Serializer.cjs +1 -1
  52. package/build/serializer/Serializer.mjs +1 -1
  53. package/build/transport/H3Transport.cjs +1 -1
  54. package/build/transport/H3Transport.mjs +1 -1
  55. package/build/transport/WebSocketTransport.cjs +16 -5
  56. package/build/transport/WebSocketTransport.cjs.map +1 -1
  57. package/build/transport/WebSocketTransport.mjs +16 -5
  58. package/build/transport/WebSocketTransport.mjs.map +1 -1
  59. package/dist/colyseus-cocos-creator.js +118 -49
  60. package/dist/colyseus-cocos-creator.js.map +1 -1
  61. package/dist/colyseus.js +118 -49
  62. package/dist/colyseus.js.map +1 -1
  63. package/dist/debug.js +284 -143
  64. package/dist/debug.js.map +1 -1
  65. package/package.json +4 -4
  66. package/src/Client.ts +2 -2
  67. package/src/Connection.ts +30 -0
  68. package/src/Room.ts +13 -10
  69. package/src/debug.ts +246 -111
  70. package/src/errors/Errors.ts +0 -11
  71. package/src/index.ts +2 -2
  72. package/src/transport/WebSocketTransport.ts +16 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/sdk",
3
- "version": "0.17.13",
3
+ "version": "0.17.15",
4
4
  "description": "Colyseus Multiplayer SDK for JavaScript/TypeScript",
5
5
  "author": "Endel Dreyer",
6
6
  "license": "MIT",
@@ -52,10 +52,10 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@colyseus/msgpackr": "^1.11.2",
55
- "@colyseus/schema": "^4.0.1",
55
+ "@colyseus/schema": "^4.0.4",
56
56
  "tslib": "^2.1.0",
57
57
  "ws": "^8.13.0",
58
- "@colyseus/shared-types": "^0.17.0",
58
+ "@colyseus/shared-types": "^0.17.1",
59
59
  "@colyseus/better-call": "^1.0.26"
60
60
  },
61
61
  "devDependencies": {
@@ -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.10"
85
+ "@colyseus/core": "^0.17.12"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "@colyseus/core": "0.17.x"
package/src/Client.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { SDKTypes, ServerRoomLike, ISeatReservation } from '@colyseus/shared-types';
1
+ import { CloseCode, type SDKTypes, type ServerRoomLike, type ISeatReservation } from '@colyseus/shared-types';
2
2
 
3
- import { CloseCode, ServerError } from './errors/Errors.ts';
3
+ import { ServerError } from './errors/Errors.ts';
4
4
  import { Room } from './Room.ts';
5
5
  import { SchemaConstructor } from './serializer/SchemaSerializer.ts';
6
6
  import { HTTP } from './HTTP.ts';
package/src/Connection.ts CHANGED
@@ -1,6 +1,20 @@
1
1
  import type { ITransport, ITransportEventMap } from "./transport/ITransport.ts";
2
2
  import { H3TransportTransport } from "./transport/H3Transport.ts";
3
3
  import { WebSocketTransport } from "./transport/WebSocketTransport.ts";
4
+ import { CloseCode } from "@colyseus/shared-types";
5
+
6
+ const onOfflineListeners: (() => void)[] = [];
7
+ const hasGlobalEventListeners = typeof (addEventListener) === "function" && typeof (removeEventListener) === "function";
8
+ if (hasGlobalEventListeners) {
9
+ /**
10
+ * Detects when the network is offline and closes all connections.
11
+ * (When switching wifi networks, etc.)
12
+ */
13
+ addEventListener("offline", () => {
14
+ console.warn(`@colyseus/sdk: 🛑 Network offline. Closing ${onOfflineListeners.length} connection(s)`);
15
+ onOfflineListeners.forEach((listener) => listener());
16
+ }, false);
17
+ }
4
18
 
5
19
  export class Connection implements ITransport {
6
20
  transport: ITransport;
@@ -9,6 +23,8 @@ export class Connection implements ITransport {
9
23
  url?: string;
10
24
  options?: any;
11
25
 
26
+ #_offlineListener = (hasGlobalEventListeners) ? () => this.close(CloseCode.MAY_TRY_RECONNECT) : null;
27
+
12
28
  constructor(protocol?: string) {
13
29
  switch (protocol) {
14
30
  case "h3":
@@ -22,6 +38,20 @@ export class Connection implements ITransport {
22
38
  }
23
39
 
24
40
  connect(url: string, options?: any): void {
41
+ if (hasGlobalEventListeners) {
42
+ const onOpen = this.events.onopen;
43
+ this.events.onopen = (ev: any) => {
44
+ onOfflineListeners.push(this.#_offlineListener);
45
+ onOpen?.(ev);
46
+ };
47
+
48
+ const onClose = this.events.onclose;
49
+ this.events.onclose = (ev: any) => {
50
+ onOfflineListeners.splice(onOfflineListeners.indexOf(this.#_offlineListener), 1);
51
+ onClose?.(ev);
52
+ };
53
+ }
54
+
25
55
  this.url = url;
26
56
  this.options = options;
27
57
  this.transport.connect(url, options);
package/src/Room.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type InferState, type ExtractRoomMessages, type ExtractRoomClientMessages, type ExtractMessageType } from '@colyseus/shared-types';
1
+ import { CloseCode, type InferState, type ExtractRoomMessages, type ExtractRoomClientMessages, type ExtractMessageType } from '@colyseus/shared-types';
2
2
  import { decode, encode, Iterator, Schema } from '@colyseus/schema';
3
3
 
4
4
  import { Packr, unpack } from '@colyseus/msgpackr';
@@ -14,7 +14,6 @@ import { createSignal } from './core/signal.ts';
14
14
 
15
15
  import { SchemaConstructor, SchemaSerializer } from './serializer/SchemaSerializer.ts';
16
16
 
17
- import { CloseCode } from './errors/Errors.ts';
18
17
  import { now } from './core/utils.ts';
19
18
 
20
19
  // Infer serializer type based on State: SchemaSerializer for Schema types, Serializer otherwise
@@ -113,7 +112,7 @@ export class Room<
113
112
  // reconnection logic
114
113
  public reconnection: ReconnectionOptions = {
115
114
  retryCount: 0,
116
- maxRetries: 8,
115
+ maxRetries: 15,
117
116
  delay: 100,
118
117
  minDelay: 100,
119
118
  maxDelay: 5000,
@@ -146,7 +145,6 @@ export class Room<
146
145
  (this.serializer as SchemaSerializer).state = new rootSchema();
147
146
  }
148
147
 
149
- this.onError((code, message) => console.warn?.(`colyseus.js - onError => (${code}) ${message}`));
150
148
  this.onLeave(() => this.removeAllListeners());
151
149
  }
152
150
 
@@ -164,7 +162,7 @@ export class Room<
164
162
  e.code === CloseCode.NO_STATUS_RECEIVED ||
165
163
  e.code === CloseCode.ABNORMAL_CLOSURE ||
166
164
  e.code === CloseCode.GOING_AWAY ||
167
- e.code === CloseCode.DEVMODE_RESTART
165
+ e.code === CloseCode.MAY_TRY_RECONNECT
168
166
  ) {
169
167
  this.onDrop.invoke(e.code, e.reason);
170
168
  this.handleReconnection();
@@ -176,7 +174,6 @@ export class Room<
176
174
  };
177
175
 
178
176
  this.connection.events.onerror = (e: CloseEvent) => {
179
- console.warn?.(`Room, onError (${e.code}): ${e.reason}`);
180
177
  this.onError.invoke(e.code, e.reason);
181
178
  };
182
179
 
@@ -227,6 +224,11 @@ export class Room<
227
224
  }
228
225
 
229
226
  public ping(callback: (ms: number) => void) {
227
+ // skip if connection is not open
228
+ if (!this.connection?.isOpen) {
229
+ return;
230
+ }
231
+
230
232
  this.#lastPingTime = now();
231
233
  this.#pingCallback = callback;
232
234
  this.packr.buffer[0] = Protocol.PING;
@@ -414,8 +416,9 @@ export class Room<
414
416
  : decode.number(buffer as Buffer, it);
415
417
 
416
418
  this.dispatchMessage(type, buffer.subarray(it.offset));
419
+
417
420
  } else if (code === Protocol.PING) {
418
- this.#pingCallback?.(now() - this.#lastPingTime);
421
+ this.#pingCallback?.(Math.round(now() - this.#lastPingTime));
419
422
  this.#pingCallback = undefined;
420
423
  }
421
424
  }
@@ -459,7 +462,6 @@ export class Room<
459
462
  }
460
463
 
461
464
  if (!this.reconnection.isReconnecting) {
462
- console.info(`[Colyseus reconnection]: ${String.fromCodePoint(0x1F504)} Re-establishing connection with roomId '${this.roomId}'...`); // 🔄
463
465
  this.reconnection.retryCount = 0;
464
466
  this.reconnection.isReconnecting = true;
465
467
  }
@@ -471,18 +473,19 @@ export class Room<
471
473
  this.reconnection.retryCount++;
472
474
 
473
475
  const delay = Math.min(this.reconnection.maxDelay, Math.max(this.reconnection.minDelay, this.reconnection.backoff(this.reconnection.retryCount, this.reconnection.delay)));
474
-
475
- console.info(`[Colyseus reconnection]: ${String.fromCodePoint(0x1F504)} will retry in ${delay}ms... (${this.reconnection.retryCount} out of ${this.reconnection.maxRetries})`); // 🔄
476
+ console.info(`[Colyseus reconnection]: ${String.fromCodePoint(0x023F3)} will retry in ${(delay/1000).toFixed(1)} seconds...`); // 🔄
476
477
 
477
478
  // Wait before attempting reconnection
478
479
  setTimeout(() => {
479
480
  try {
481
+ console.info(`[Colyseus reconnection]: ${String.fromCodePoint(0x1F504)} Re-establishing sessionId '${this.sessionId}' with roomId '${this.roomId}'... (attempt ${this.reconnection.retryCount} of ${this.reconnection.maxRetries})`); // 🔄
480
482
  this.connection.reconnect({
481
483
  reconnectionToken: this.reconnectionToken.split(":")[1],
482
484
  skipHandshake: true, // we already applied the handshake on first join
483
485
  });
484
486
 
485
487
  } catch (e) {
488
+ console.log(".reconnect() failed", e);
486
489
  if (this.reconnection.retryCount < this.reconnection.maxRetries) {
487
490
  this.retryReconnection();
488
491
  } else {