@colyseus/core 0.15.45-preview.0 → 0.16.0-alpha.21
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 +5 -5
- package/build/IPC.js.map +1 -1
- package/build/IPC.mjs.map +1 -1
- package/build/MatchMaker.d.ts +20 -15
- package/build/MatchMaker.js +63 -78
- package/build/MatchMaker.js.map +2 -2
- package/build/MatchMaker.mjs +62 -75
- package/build/MatchMaker.mjs.map +2 -2
- package/build/Protocol.d.ts +3 -9
- package/build/Protocol.js +35 -95
- package/build/Protocol.js.map +2 -2
- package/build/Protocol.mjs +34 -92
- package/build/Protocol.mjs.map +2 -2
- package/build/Room.d.ts +22 -27
- package/build/Room.js +55 -67
- package/build/Room.js.map +3 -3
- package/build/Room.mjs +56 -68
- package/build/Room.mjs.map +2 -2
- package/build/Server.d.ts +0 -1
- package/build/Server.js +4 -4
- package/build/Server.js.map +2 -2
- package/build/Server.mjs +3 -3
- package/build/Server.mjs.map +2 -2
- package/build/Transport.d.ts +37 -17
- package/build/Transport.js.map +2 -2
- package/build/Transport.mjs.map +2 -2
- package/build/index.d.ts +3 -3
- package/build/index.js +3 -3
- package/build/index.js.map +2 -2
- package/build/index.mjs +3 -3
- package/build/index.mjs.map +2 -2
- package/build/matchmaker/Lobby.d.ts +2 -2
- package/build/matchmaker/Lobby.js.map +2 -2
- package/build/matchmaker/Lobby.mjs.map +2 -2
- package/build/matchmaker/RegisteredHandler.d.ts +4 -5
- package/build/matchmaker/RegisteredHandler.js +4 -3
- package/build/matchmaker/RegisteredHandler.js.map +2 -2
- package/build/matchmaker/RegisteredHandler.mjs +4 -3
- package/build/matchmaker/RegisteredHandler.mjs.map +2 -2
- package/build/matchmaker/controller.d.ts +1 -2
- package/build/matchmaker/driver/RoomData.d.ts +3 -3
- package/build/matchmaker/driver/RoomData.js +3 -3
- package/build/matchmaker/driver/RoomData.js.map +2 -2
- package/build/matchmaker/driver/RoomData.mjs +2 -2
- package/build/matchmaker/driver/RoomData.mjs.map +2 -2
- package/build/matchmaker/driver/api.d.ts +104 -0
- package/build/matchmaker/driver/api.js +29 -0
- package/build/matchmaker/driver/api.js.map +7 -0
- package/build/matchmaker/driver/api.mjs +6 -0
- package/build/matchmaker/driver/api.mjs.map +7 -0
- package/build/matchmaker/driver/index.d.ts +7 -7
- package/build/matchmaker/driver/index.js +1 -1
- package/build/matchmaker/driver/index.js.map +2 -2
- package/build/matchmaker/driver/index.mjs +2 -2
- package/build/matchmaker/driver/index.mjs.map +2 -2
- package/build/matchmaker/driver/interfaces.d.ts +7 -11
- package/build/matchmaker/driver/interfaces.js.map +1 -1
- package/build/matchmaker/driver/local/LocalDriver.d.ts +13 -0
- package/build/matchmaker/driver/local/LocalDriver.js +65 -0
- package/build/matchmaker/driver/local/LocalDriver.js.map +7 -0
- package/build/matchmaker/driver/local/LocalDriver.mjs +42 -0
- package/build/matchmaker/driver/local/LocalDriver.mjs.map +7 -0
- package/build/matchmaker/driver/local/Query.d.ts +9 -0
- package/build/matchmaker/driver/local/Query.js +78 -0
- package/build/matchmaker/driver/local/Query.js.map +7 -0
- package/build/matchmaker/driver/local/Query.mjs +55 -0
- package/build/matchmaker/driver/local/Query.mjs.map +7 -0
- package/build/matchmaker/driver/local/RoomData.d.ts +19 -0
- package/build/matchmaker/driver/local/RoomData.js +79 -0
- package/build/matchmaker/driver/local/RoomData.js.map +7 -0
- package/build/matchmaker/driver/local/RoomData.mjs +56 -0
- package/build/matchmaker/driver/local/RoomData.mjs.map +7 -0
- package/build/presence/LocalPresence.d.ts +6 -1
- package/build/presence/LocalPresence.js +61 -0
- package/build/presence/LocalPresence.js.map +3 -3
- package/build/presence/LocalPresence.mjs +61 -0
- package/build/presence/LocalPresence.mjs.map +3 -3
- package/build/presence/Presence.d.ts +6 -0
- package/build/presence/Presence.js.map +1 -1
- package/build/rooms/LobbyRoom.d.ts +4 -4
- package/build/rooms/LobbyRoom.js.map +2 -2
- package/build/rooms/LobbyRoom.mjs.map +2 -2
- package/build/rooms/RelayRoom.js +2 -3
- package/build/rooms/RelayRoom.js.map +2 -2
- package/build/rooms/RelayRoom.mjs +3 -4
- package/build/rooms/RelayRoom.mjs.map +2 -2
- package/build/serializer/SchemaSerializer.d.ts +14 -7
- package/build/serializer/SchemaSerializer.js +66 -38
- package/build/serializer/SchemaSerializer.js.map +2 -2
- package/build/serializer/SchemaSerializer.mjs +67 -39
- package/build/serializer/SchemaSerializer.mjs.map +2 -2
- package/build/serializer/Serializer.d.ts +2 -2
- package/build/serializer/Serializer.js.map +1 -1
- package/build/utils/DevMode.js +1 -1
- package/build/utils/DevMode.js.map +2 -2
- package/build/utils/DevMode.mjs +2 -2
- package/build/utils/DevMode.mjs.map +2 -2
- package/build/utils/Utils.d.ts +2 -9
- package/build/utils/Utils.js +7 -4
- package/build/utils/Utils.js.map +2 -2
- package/build/utils/Utils.mjs +5 -3
- package/build/utils/Utils.mjs.map +2 -2
- package/package.json +5 -6
package/build/Protocol.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/Protocol.ts"],
|
|
4
|
-
"sourcesContent": ["import { pack } from 'msgpackr';\nimport { encode,
|
|
5
|
-
"mappings": "AAAA,
|
|
4
|
+
"sourcesContent": ["import { pack, Packr } from '@colyseus/msgpackr';\nimport { encode, Iterator } from '@colyseus/schema';\n\n// Colyseus protocol codes range between 0~100\nexport enum Protocol {\n // Room-related (10~19)\n JOIN_ROOM = 10,\n ERROR = 11,\n LEAVE_ROOM = 12,\n ROOM_DATA = 13,\n ROOM_STATE = 14,\n ROOM_STATE_PATCH = 15,\n // ROOM_DATA_SCHEMA = 16, // DEPRECATED: used to send schema instances via room.send()\n ROOM_DATA_BYTES = 17,\n\n // WebSocket close codes (https://github.com/Luka967/websocket-close-codes)\n WS_CLOSE_NORMAL = 1000,\n WS_CLOSE_GOING_AWAY = 1001,\n\n // WebSocket error codes\n WS_CLOSE_CONSENTED = 4000,\n WS_CLOSE_WITH_ERROR = 4002,\n WS_CLOSE_DEVMODE_RESTART = 4010,\n\n WS_SERVER_DISCONNECT = 4201,\n WS_TOO_MANY_CLIENTS = 4202,\n}\n\nexport enum ErrorCode {\n // MatchMaking Error Codes\n MATCHMAKE_NO_HANDLER = 4210,\n MATCHMAKE_INVALID_CRITERIA = 4211,\n MATCHMAKE_INVALID_ROOM_ID = 4212,\n MATCHMAKE_UNHANDLED = 4213, // generic exception during onCreate/onJoin\n MATCHMAKE_EXPIRED = 4214, // generic exception during onCreate/onJoin\n\n AUTH_FAILED = 4215,\n APPLICATION_ERROR = 4216,\n\n INVALID_PAYLOAD = 4217,\n}\n\n// Inter-process communication protocol\nexport enum IpcProtocol {\n SUCCESS = 0,\n ERROR = 1,\n TIMEOUT = 2,\n}\n\nconst sendBuffer = Buffer.allocUnsafe(8192);\n\nconst packr = new Packr();\npackr.useBuffer(sendBuffer);\n\nexport const getMessageBytes = {\n [Protocol.JOIN_ROOM]: (reconnectionToken: string, serializerId: string, handshake?: Buffer) => {\n const it: Iterator = { offset: 1 };\n sendBuffer[0] = Protocol.JOIN_ROOM;\n\n sendBuffer[it.offset++] = Buffer.byteLength(reconnectionToken, \"utf8\");\n encode.utf8Write(sendBuffer, reconnectionToken, it);\n\n sendBuffer[it.offset++] = Buffer.byteLength(serializerId, \"utf8\");\n encode.utf8Write(sendBuffer, serializerId, it);\n\n let handshakeLength = handshake?.byteLength || 0;\n if (handshakeLength > 0) {\n handshake.copy(sendBuffer, it.offset, 0, handshakeLength);\n }\n\n return sendBuffer.subarray(0, it.offset + handshakeLength);\n },\n\n [Protocol.ERROR]: (code: number, message: string = '') => {\n const it: Iterator = { offset: 1 };\n sendBuffer[0] = Protocol.ERROR;\n\n encode.number(sendBuffer, code, it);\n encode.string(sendBuffer, message, it);\n\n return sendBuffer.subarray(0, it.offset);\n },\n\n [Protocol.ROOM_STATE]: (bytes: number[]) => {\n return [Protocol.ROOM_STATE, ...bytes];\n },\n\n raw: (code: Protocol, type: string | number, message?: any, rawMessage?: Uint8Array | Buffer) => {\n const it: Iterator = { offset: 1 };\n sendBuffer[0] = code;\n\n if (typeof (type) === 'string') {\n encode.string(sendBuffer, type as string, it);\n\n } else {\n encode.number(sendBuffer, type, it);\n }\n\n if (message !== undefined) {\n // force to encode from offset\n packr.position = 0;\n\n //\n // TODO: remove this after issue is fixed https://github.com/kriszyp/msgpackr/issues/139\n //\n // - This check is only required when running integration tests.\n // (colyseus.js' usage of msgpackr/buffer is conflicting)\n //\n if (process.env.NODE_ENV !== \"production\") {\n packr.useBuffer(sendBuffer);\n }\n\n // pack message into the same sendBuffer\n const endOfBufferOffset = packr.pack(message, 2048 + it.offset).byteLength;\n // 2048 = RESERVE_START_SPACE\n return sendBuffer.subarray(0, endOfBufferOffset);\n\n } else if (rawMessage !== undefined) {\n\n // copy raw message into sendBuffer\n sendBuffer.set(rawMessage, it.offset);\n return sendBuffer.subarray(0, it.offset + rawMessage.byteLength);\n\n } else {\n return sendBuffer.subarray(0, it.offset);\n }\n },\n\n};\n\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
6
|
"names": ["Protocol", "ErrorCode", "IpcProtocol"]
|
|
7
7
|
}
|
package/build/Room.d.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="node" />
|
|
4
1
|
import http, { IncomingMessage } from 'http';
|
|
5
|
-
import { Schema } from '@colyseus/schema';
|
|
6
2
|
import Clock from '@gamestdio/timer';
|
|
7
3
|
import { EventEmitter } from 'events';
|
|
8
4
|
import { Presence } from './presence/Presence';
|
|
9
5
|
import { Serializer } from './serializer/Serializer';
|
|
10
6
|
import { Deferred } from './utils/Utils';
|
|
11
|
-
import {
|
|
12
|
-
import { Client, ClientArray, ISendOptions } from './Transport';
|
|
7
|
+
import { RoomCache } from './matchmaker/driver/local/LocalDriver';
|
|
8
|
+
import { Client, ClientArray, ClientPrivate, ISendOptions } from './Transport';
|
|
13
9
|
export declare const DEFAULT_SEAT_RESERVATION_TIME: number;
|
|
14
10
|
export type SimulationCallback = (deltaTime: number) => void;
|
|
15
11
|
export interface IBroadcastOptions extends ISendOptions {
|
|
@@ -20,8 +16,6 @@ export declare enum RoomInternalState {
|
|
|
20
16
|
CREATED = 1,
|
|
21
17
|
DISPOSING = 2
|
|
22
18
|
}
|
|
23
|
-
type ExtractUserData<T> = T extends ClientArray<infer U> ? U : never;
|
|
24
|
-
type ExtractAuthData<T> = T extends ClientArray<infer _, infer U> ? U : never;
|
|
25
19
|
/**
|
|
26
20
|
* A Room class is meant to implement a game session, and/or serve as the communication channel
|
|
27
21
|
* between a group of clients.
|
|
@@ -29,7 +23,7 @@ type ExtractAuthData<T> = T extends ClientArray<infer _, infer U> ? U : never;
|
|
|
29
23
|
* - Rooms are created on demand during matchmaking by default
|
|
30
24
|
* - Room classes must be exposed using `.define()`
|
|
31
25
|
*/
|
|
32
|
-
export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
26
|
+
export declare abstract class Room<State extends object = any, Metadata = any, UserData = any, AuthData = any> {
|
|
33
27
|
#private;
|
|
34
28
|
/**
|
|
35
29
|
* This property will change on these situations:
|
|
@@ -40,9 +34,10 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
40
34
|
*/
|
|
41
35
|
get locked(): boolean;
|
|
42
36
|
get metadata(): Metadata;
|
|
43
|
-
listing:
|
|
37
|
+
listing: RoomCache<Metadata>;
|
|
44
38
|
/**
|
|
45
|
-
*
|
|
39
|
+
* Timing events tied to the room instance.
|
|
40
|
+
* Intervals and timeouts are cleared when the room is disposed.
|
|
46
41
|
*/
|
|
47
42
|
clock: Clock;
|
|
48
43
|
/**
|
|
@@ -72,15 +67,15 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
72
67
|
*
|
|
73
68
|
* @see {@link https://docs.colyseus.io/colyseus/server/room/#client|Client instance}
|
|
74
69
|
*/
|
|
75
|
-
clients: ClientArray
|
|
70
|
+
clients: ClientArray<UserData, AuthData>;
|
|
76
71
|
/** @internal */
|
|
77
72
|
_events: EventEmitter<[never]>;
|
|
78
73
|
protected seatReservationTime: number;
|
|
79
74
|
protected reservedSeats: {
|
|
80
|
-
[sessionId: string]: [any, any
|
|
75
|
+
[sessionId: string]: [any, any];
|
|
81
76
|
};
|
|
82
77
|
protected reservedSeatTimeouts: {
|
|
83
|
-
[sessionId: string]: NodeJS.
|
|
78
|
+
[sessionId: string]: NodeJS.Timeout;
|
|
84
79
|
};
|
|
85
80
|
protected _reconnections: {
|
|
86
81
|
[reconnectionToken: string]: [string, Deferred];
|
|
@@ -96,7 +91,7 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
96
91
|
private _lockedExplicitly;
|
|
97
92
|
private _maxClientsReached;
|
|
98
93
|
private _autoDisposeTimeout;
|
|
99
|
-
constructor(
|
|
94
|
+
constructor();
|
|
100
95
|
/**
|
|
101
96
|
* Automatically dispose the room when last client disconnects.
|
|
102
97
|
*
|
|
@@ -132,14 +127,14 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
132
127
|
set roomId(roomId: string);
|
|
133
128
|
onBeforePatch?(state: State): void | Promise<any>;
|
|
134
129
|
onCreate?(options: any): void | Promise<any>;
|
|
135
|
-
onJoin?(client: Client<
|
|
136
|
-
onLeave?(client: Client<
|
|
130
|
+
onJoin?(client: Client<UserData, AuthData>, options?: any, auth?: AuthData): void | Promise<any>;
|
|
131
|
+
onLeave?(client: Client<UserData, AuthData>, consented?: boolean): void | Promise<any>;
|
|
137
132
|
onDispose?(): void | Promise<any>;
|
|
138
133
|
/**
|
|
139
134
|
* onAuth at the instance level will be deprecated in the future.
|
|
140
135
|
* Please use "static onAuth(token, req) instead
|
|
141
136
|
*/
|
|
142
|
-
onAuth(client: Client<
|
|
137
|
+
onAuth(client: Client<UserData, AuthData>, options: any, request?: http.IncomingMessage): any | Promise<any>;
|
|
143
138
|
static onAuth(token: string, req: IncomingMessage): Promise<unknown>;
|
|
144
139
|
/**
|
|
145
140
|
* devMode: When `devMode` is enabled, `onCacheRoom` method is called during
|
|
@@ -199,15 +194,17 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
199
194
|
*/
|
|
200
195
|
unlock(): Promise<void>;
|
|
201
196
|
send(client: Client, type: string | number, message: any, options?: ISendOptions): void;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
197
|
+
broadcast(type: string | number, message?: any, options?: IBroadcastOptions): void;
|
|
198
|
+
/**
|
|
199
|
+
* Broadcast bytes (UInt8Arrays) to a particular room
|
|
200
|
+
*/
|
|
201
|
+
broadcastBytes(type: string | number, message: Uint8Array, options: IBroadcastOptions): void;
|
|
205
202
|
/**
|
|
206
203
|
* Checks whether mutations have occurred in the state, and broadcast them to all connected clients.
|
|
207
204
|
*/
|
|
208
205
|
broadcastPatch(): boolean;
|
|
209
|
-
onMessage<T = any>(messageType: '*', callback: (client: Client<
|
|
210
|
-
onMessage<T = any>(messageType: string | number, callback: (client: Client<
|
|
206
|
+
onMessage<T = any>(messageType: '*', callback: (client: Client<UserData, AuthData>, type: string | number, message: T) => void): any;
|
|
207
|
+
onMessage<T = any>(messageType: string | number, callback: (client: Client<UserData, AuthData>, message: T) => void): any;
|
|
211
208
|
/**
|
|
212
209
|
* Disconnect all connected clients, and then dispose the room.
|
|
213
210
|
*
|
|
@@ -215,7 +212,7 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
215
212
|
* @returns Promise<void>
|
|
216
213
|
*/
|
|
217
214
|
disconnect(closeCode?: number): Promise<any>;
|
|
218
|
-
['_onJoin'](client: Client, req?: http.IncomingMessage): Promise<void>;
|
|
215
|
+
['_onJoin'](client: Client & ClientPrivate, req?: http.IncomingMessage): Promise<void>;
|
|
219
216
|
/**
|
|
220
217
|
* Allow the specified client to reconnect into the room. Must be used inside `onLeave()` method.
|
|
221
218
|
* If seconds is provided, the reconnection is going to be cancelled after the provided amount of seconds.
|
|
@@ -228,9 +225,8 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
228
225
|
*/
|
|
229
226
|
allowReconnection(previousClient: Client, seconds: number | "manual"): Deferred<Client>;
|
|
230
227
|
protected resetAutoDisposeTimeout(timeoutInSeconds?: number): void;
|
|
231
|
-
private broadcastMessageSchema;
|
|
232
228
|
private broadcastMessageType;
|
|
233
|
-
|
|
229
|
+
private sendFullState;
|
|
234
230
|
private _dequeueAfterPatchMessages;
|
|
235
231
|
private _reserveSeat;
|
|
236
232
|
private _disposeIfEmpty;
|
|
@@ -241,4 +237,3 @@ export declare abstract class Room<State extends object = any, Metadata = any> {
|
|
|
241
237
|
private _incrementClientCount;
|
|
242
238
|
private _decrementClientCount;
|
|
243
239
|
}
|
|
244
|
-
export {};
|
package/build/Room.js
CHANGED
|
@@ -28,8 +28,7 @@ __export(Room_exports, {
|
|
|
28
28
|
RoomInternalState: () => RoomInternalState
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(Room_exports);
|
|
31
|
-
var
|
|
32
|
-
var import_msgpackr = require("msgpackr");
|
|
31
|
+
var import_msgpackr = require("@colyseus/msgpackr");
|
|
33
32
|
var import_schema = require("@colyseus/schema");
|
|
34
33
|
var import_timer = __toESM(require("@gamestdio/timer"));
|
|
35
34
|
var import_events = require("events");
|
|
@@ -53,7 +52,7 @@ var RoomInternalState = /* @__PURE__ */ ((RoomInternalState2) => {
|
|
|
53
52
|
return RoomInternalState2;
|
|
54
53
|
})(RoomInternalState || {});
|
|
55
54
|
class Room {
|
|
56
|
-
constructor(
|
|
55
|
+
constructor() {
|
|
57
56
|
this.clock = new import_timer.default();
|
|
58
57
|
this.#_autoDispose = true;
|
|
59
58
|
this.maxClients = Infinity;
|
|
@@ -72,9 +71,13 @@ class Room {
|
|
|
72
71
|
this._locked = false;
|
|
73
72
|
this._lockedExplicitly = false;
|
|
74
73
|
this._maxClientsReached = false;
|
|
75
|
-
this.
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
this._events.once("dispose", async () => {
|
|
75
|
+
try {
|
|
76
|
+
await this._dispose();
|
|
77
|
+
} catch (e) {
|
|
78
|
+
(0, import_Debug.debugAndPrintError)(`onDispose error: ${e && e.message || e || "promise rejected"}`);
|
|
79
|
+
}
|
|
80
|
+
this._events.emit("disconnect");
|
|
78
81
|
});
|
|
79
82
|
this.setPatchRate(this.patchRate);
|
|
80
83
|
this.resetAutoDisposeTimeout(this.seatReservationTime);
|
|
@@ -129,20 +132,17 @@ class Room {
|
|
|
129
132
|
return this;
|
|
130
133
|
}
|
|
131
134
|
hasReservedSeat(sessionId, reconnectionToken) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return
|
|
135
|
-
}
|
|
136
|
-
if (reservedSeat[3]) {
|
|
137
|
-
return reconnectionToken && this._reconnections[reconnectionToken]?.[0] === sessionId && this._reconnectingSessionId.has(sessionId);
|
|
135
|
+
if (reconnectionToken) {
|
|
136
|
+
const reconnection = this._reconnections[reconnectionToken];
|
|
137
|
+
return reconnection && reconnection[0] === sessionId && this.reservedSeats[sessionId] !== void 0 && this._reconnectingSessionId.has(sessionId);
|
|
138
138
|
} else {
|
|
139
|
-
return
|
|
139
|
+
return this.reservedSeats[sessionId] !== void 0 && (!this._reconnectingSessionId.has(sessionId) || this._reconnectingSessionId.get(sessionId) === sessionId);
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
checkReconnectionToken(reconnectionToken) {
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
143
|
+
const reconnection = this._reconnections[reconnectionToken];
|
|
144
|
+
const sessionId = reconnection && reconnection[0];
|
|
145
|
+
if (this.hasReservedSeat(sessionId)) {
|
|
146
146
|
this._reconnectingSessionId.set(sessionId, reconnectionToken);
|
|
147
147
|
return sessionId;
|
|
148
148
|
} else {
|
|
@@ -172,8 +172,10 @@ class Room {
|
|
|
172
172
|
}
|
|
173
173
|
setState(newState) {
|
|
174
174
|
this.clock.start();
|
|
175
|
-
if (
|
|
175
|
+
if (newState[import_schema.$changes] !== void 0) {
|
|
176
176
|
this.setSerializer(new import_SchemaSerializer.SchemaSerializer());
|
|
177
|
+
} else if (import_schema.$changes === void 0) {
|
|
178
|
+
throw new Error("@colyseus/schema v2 compatibility currently missing (reach out if you need it)");
|
|
177
179
|
}
|
|
178
180
|
this._serializer.reset(newState);
|
|
179
181
|
this.state = newState;
|
|
@@ -236,19 +238,21 @@ class Room {
|
|
|
236
238
|
import_Logger.logger.warn("DEPRECATION WARNING: use client.send(...) instead of this.send(client, ...)");
|
|
237
239
|
client.send(messageOrType, messageOrOptions, options);
|
|
238
240
|
}
|
|
239
|
-
broadcast(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (opts && opts.afterNextPatch) {
|
|
243
|
-
delete opts.afterNextPatch;
|
|
241
|
+
broadcast(type, message, options) {
|
|
242
|
+
if (options && options.afterNextPatch) {
|
|
243
|
+
delete options.afterNextPatch;
|
|
244
244
|
this._afterNextPatchQueue.push(["broadcast", arguments]);
|
|
245
245
|
return;
|
|
246
246
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
247
|
+
this.broadcastMessageType(type, message, options);
|
|
248
|
+
}
|
|
249
|
+
broadcastBytes(type, message, options) {
|
|
250
|
+
if (options && options.afterNextPatch) {
|
|
251
|
+
delete options.afterNextPatch;
|
|
252
|
+
this._afterNextPatchQueue.push(["broadcastBytes", arguments]);
|
|
253
|
+
return;
|
|
251
254
|
}
|
|
255
|
+
this.broadcastMessageType(type, message, options);
|
|
252
256
|
}
|
|
253
257
|
broadcastPatch() {
|
|
254
258
|
if (this.onBeforePatch) {
|
|
@@ -293,7 +297,7 @@ class Room {
|
|
|
293
297
|
}
|
|
294
298
|
async ["_onJoin"](client, req) {
|
|
295
299
|
const sessionId = client.sessionId;
|
|
296
|
-
client.
|
|
300
|
+
client.reconnectionToken = (0, import_Utils.generateId)();
|
|
297
301
|
if (this.reservedSeatTimeouts[sessionId]) {
|
|
298
302
|
clearTimeout(this.reservedSeatTimeouts[sessionId]);
|
|
299
303
|
delete this.reservedSeatTimeouts[sessionId];
|
|
@@ -302,23 +306,18 @@ class Room {
|
|
|
302
306
|
clearTimeout(this._autoDisposeTimeout);
|
|
303
307
|
this._autoDisposeTimeout = void 0;
|
|
304
308
|
}
|
|
305
|
-
const [joinOptions, authData
|
|
306
|
-
if (
|
|
309
|
+
const [joinOptions, authData] = this.reservedSeats[sessionId];
|
|
310
|
+
if (this.reservedSeats[sessionId].length > 2) {
|
|
307
311
|
throw new import_ServerError.ServerError(import_Protocol.ErrorCode.MATCHMAKE_EXPIRED, "already consumed");
|
|
308
312
|
}
|
|
309
|
-
this.reservedSeats[sessionId]
|
|
313
|
+
this.reservedSeats[sessionId].push(true);
|
|
310
314
|
client._afterNextPatchQueue = this._afterNextPatchQueue;
|
|
311
315
|
client.ref["onleave"] = (_) => client.state = import_Transport.ClientState.LEAVING;
|
|
312
316
|
client.ref.once("close", client.ref["onleave"]);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
this._reconnections[previousReconnectionToken]?.[1].resolve(client);
|
|
318
|
-
} else {
|
|
319
|
-
const errorMessage = process.env.NODE_ENV === "production" ? "already consumed" : "bad reconnection token";
|
|
320
|
-
throw new import_ServerError.ServerError(import_Protocol.ErrorCode.MATCHMAKE_EXPIRED, errorMessage);
|
|
321
|
-
}
|
|
317
|
+
const previousReconnectionToken = this._reconnectingSessionId.get(sessionId);
|
|
318
|
+
if (previousReconnectionToken) {
|
|
319
|
+
this.clients.push(client);
|
|
320
|
+
this._reconnections[previousReconnectionToken]?.[1].resolve(client);
|
|
322
321
|
} else {
|
|
323
322
|
try {
|
|
324
323
|
if (authData) {
|
|
@@ -329,7 +328,7 @@ class Room {
|
|
|
329
328
|
throw new import_ServerError.ServerError(import_Protocol.ErrorCode.AUTH_FAILED, "onAuth failed");
|
|
330
329
|
}
|
|
331
330
|
}
|
|
332
|
-
if (client.
|
|
331
|
+
if (client.state === import_Transport.ClientState.LEAVING) {
|
|
333
332
|
throw new import_ServerError.ServerError(import_Protocol.Protocol.WS_CLOSE_GOING_AWAY, "already disconnected");
|
|
334
333
|
}
|
|
335
334
|
this.clients.push(client);
|
|
@@ -361,7 +360,7 @@ class Room {
|
|
|
361
360
|
client.ref.once("close", client.ref["onleave"]);
|
|
362
361
|
client.ref.on("message", this._onMessage.bind(this, client));
|
|
363
362
|
client.raw(import_Protocol.getMessageBytes[import_Protocol.Protocol.JOIN_ROOM](
|
|
364
|
-
client.
|
|
363
|
+
client.reconnectionToken,
|
|
365
364
|
this._serializer.id,
|
|
366
365
|
this._serializer.handshake && this._serializer.handshake()
|
|
367
366
|
));
|
|
@@ -383,7 +382,7 @@ class Room {
|
|
|
383
382
|
throw new Error("disconnecting");
|
|
384
383
|
}
|
|
385
384
|
const sessionId = previousClient.sessionId;
|
|
386
|
-
const reconnectionToken = previousClient.
|
|
385
|
+
const reconnectionToken = previousClient.reconnectionToken;
|
|
387
386
|
this._reserveSeat(sessionId, true, previousClient.auth, seconds, true);
|
|
388
387
|
const reconnection = new import_Utils.Deferred();
|
|
389
388
|
this._reconnections[reconnectionToken] = [sessionId, reconnection];
|
|
@@ -419,21 +418,9 @@ class Room {
|
|
|
419
418
|
this._disposeIfEmpty();
|
|
420
419
|
}, timeoutInSeconds * 1e3);
|
|
421
420
|
}
|
|
422
|
-
broadcastMessageSchema(message, options = {}) {
|
|
423
|
-
(0, import_Debug.debugMessage)("broadcast: %O", message);
|
|
424
|
-
const encodedMessage = import_Protocol.getMessageBytes[import_Protocol.Protocol.ROOM_DATA_SCHEMA](message);
|
|
425
|
-
const except = typeof options.except !== "undefined" ? Array.isArray(options.except) ? options.except : [options.except] : void 0;
|
|
426
|
-
let numClients = this.clients.length;
|
|
427
|
-
while (numClients--) {
|
|
428
|
-
const client = this.clients[numClients];
|
|
429
|
-
if (!except || !except.includes(client)) {
|
|
430
|
-
client.enqueueRaw(encodedMessage);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
421
|
broadcastMessageType(type, message, options = {}) {
|
|
435
422
|
(0, import_Debug.debugMessage)("broadcast: %O", message);
|
|
436
|
-
const encodedMessage = import_Protocol.getMessageBytes.raw(import_Protocol.Protocol.ROOM_DATA, type, message);
|
|
423
|
+
const encodedMessage = message instanceof Uint8Array ? import_Protocol.getMessageBytes.raw(import_Protocol.Protocol.ROOM_DATA_BYTES, type, void 0, message) : import_Protocol.getMessageBytes.raw(import_Protocol.Protocol.ROOM_DATA, type, message);
|
|
437
424
|
const except = typeof options.except !== "undefined" ? Array.isArray(options.except) ? options.except : [options.except] : void 0;
|
|
438
425
|
let numClients = this.clients.length;
|
|
439
426
|
while (numClients--) {
|
|
@@ -444,7 +431,7 @@ class Room {
|
|
|
444
431
|
}
|
|
445
432
|
}
|
|
446
433
|
sendFullState(client) {
|
|
447
|
-
client.
|
|
434
|
+
client.raw(this._serializer.getFullState(client));
|
|
448
435
|
}
|
|
449
436
|
_dequeueAfterPatchMessages() {
|
|
450
437
|
const length = this._afterNextPatchQueue.length;
|
|
@@ -464,7 +451,7 @@ class Room {
|
|
|
464
451
|
if (!allowReconnection && this.hasReachedMaxClients()) {
|
|
465
452
|
return false;
|
|
466
453
|
}
|
|
467
|
-
this.reservedSeats[sessionId] = [joinOptions, authData
|
|
454
|
+
this.reservedSeats[sessionId] = [joinOptions, authData];
|
|
468
455
|
if (!allowReconnection) {
|
|
469
456
|
await this._incrementClientCount();
|
|
470
457
|
this.reservedSeatTimeouts[sessionId] = setTimeout(async () => {
|
|
@@ -488,7 +475,7 @@ class Room {
|
|
|
488
475
|
}
|
|
489
476
|
async _dispose() {
|
|
490
477
|
this._internalState = 2 /* DISPOSING */;
|
|
491
|
-
this.listing.remove();
|
|
478
|
+
await this.listing.remove();
|
|
492
479
|
let userReturnData;
|
|
493
480
|
if (this.onDispose) {
|
|
494
481
|
userReturnData = this.onDispose();
|
|
@@ -509,21 +496,21 @@ class Room {
|
|
|
509
496
|
this.clock.stop();
|
|
510
497
|
return await (userReturnData || Promise.resolve());
|
|
511
498
|
}
|
|
512
|
-
_onMessage(client,
|
|
499
|
+
_onMessage(client, buffer) {
|
|
513
500
|
if (client.state === import_Transport.ClientState.LEAVING) {
|
|
514
501
|
return;
|
|
515
502
|
}
|
|
516
|
-
const it = { offset:
|
|
517
|
-
const code =
|
|
518
|
-
if (!
|
|
519
|
-
(0, import_Debug.debugAndPrintError)(`${this.roomName} (${this.roomId}), couldn't decode message: ${
|
|
503
|
+
const it = { offset: 1 };
|
|
504
|
+
const code = buffer[0];
|
|
505
|
+
if (!buffer) {
|
|
506
|
+
(0, import_Debug.debugAndPrintError)(`${this.roomName} (${this.roomId}), couldn't decode message: ${buffer}`);
|
|
520
507
|
return;
|
|
521
508
|
}
|
|
522
509
|
if (code === import_Protocol.Protocol.ROOM_DATA) {
|
|
523
|
-
const messageType = import_schema.decode.stringCheck(
|
|
510
|
+
const messageType = import_schema.decode.stringCheck(buffer, it) ? import_schema.decode.string(buffer, it) : import_schema.decode.number(buffer, it);
|
|
524
511
|
let message;
|
|
525
512
|
try {
|
|
526
|
-
message =
|
|
513
|
+
message = buffer.byteLength > it.offset ? (0, import_msgpackr.unpack)(buffer.subarray(it.offset, buffer.byteLength)) : void 0;
|
|
527
514
|
(0, import_Debug.debugMessage)("received: '%s' -> %j", messageType, message);
|
|
528
515
|
} catch (e) {
|
|
529
516
|
(0, import_Debug.debugAndPrintError)(e);
|
|
@@ -544,8 +531,8 @@ class Room {
|
|
|
544
531
|
}
|
|
545
532
|
}
|
|
546
533
|
} else if (code === import_Protocol.Protocol.ROOM_DATA_BYTES) {
|
|
547
|
-
const messageType = import_schema.decode.stringCheck(
|
|
548
|
-
const message =
|
|
534
|
+
const messageType = import_schema.decode.stringCheck(buffer, it) ? import_schema.decode.string(buffer, it) : import_schema.decode.number(buffer, it);
|
|
535
|
+
const message = buffer.subarray(it.offset, buffer.byteLength);
|
|
549
536
|
(0, import_Debug.debugMessage)("received: '%s' -> %j", messageType, message);
|
|
550
537
|
if (this.onMessageHandlers[messageType]) {
|
|
551
538
|
this.onMessageHandlers[messageType](client, message);
|
|
@@ -562,6 +549,7 @@ class Room {
|
|
|
562
549
|
}
|
|
563
550
|
} else if (code === import_Protocol.Protocol.JOIN_ROOM && client.state === import_Transport.ClientState.JOINING) {
|
|
564
551
|
client.state = import_Transport.ClientState.JOINED;
|
|
552
|
+
client._joinedAt = this.clock.elapsedTime;
|
|
565
553
|
if (this.state) {
|
|
566
554
|
this.sendFullState(client);
|
|
567
555
|
}
|