@signe/room 0.0.1 → 0.0.2
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/dist/index.d.ts +195 -6
- package/dist/index.js +468 -57
- package/dist/index.js.map +1 -1
- package/examples/game/.vscode/launch.json +11 -0
- package/examples/game/.vscode/settings.json +11 -0
- package/examples/game/README.md +40 -0
- package/examples/game/app/client.tsx +40 -0
- package/examples/game/app/components/Counter.tsx +44 -0
- package/examples/game/app/styles.css +31 -0
- package/examples/game/package.json +20 -0
- package/examples/game/party/game.room.ts +12 -0
- package/examples/game/party/server.ts +12 -0
- package/examples/game/partykit.json +10 -0
- package/examples/game/public/favicon.ico +0 -0
- package/examples/game/public/index.html +27 -0
- package/examples/game/public/normalize.css +351 -0
- package/examples/game/shared/room.schema.ts +6 -0
- package/examples/game/tsconfig.json +109 -0
- package/package.json +2 -2
- package/src/decorators.ts +11 -2
- package/src/index.ts +1 -0
- package/src/mock.ts +70 -0
- package/src/server.ts +272 -48
- package/src/utils.ts +150 -24
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,43 @@
|
|
|
1
1
|
import { Request as Request$1, WebSocket, DurableObjectState, AnalyticsEngineDataset, DurableObjectStorage, VectorizeIndex, R2Bucket, KVNamespace } from '@cloudflare/workers-types';
|
|
2
2
|
|
|
3
|
-
declare function
|
|
4
|
-
|
|
3
|
+
declare function Action(name: string, bodyValidation?: any): (target: any, propertyKey: string) => void;
|
|
4
|
+
interface RoomOptions {
|
|
5
|
+
path: string;
|
|
6
|
+
maxUsers?: number;
|
|
7
|
+
throttleStorage?: number;
|
|
8
|
+
throttleSync?: number;
|
|
9
|
+
hibernate?: boolean;
|
|
10
|
+
}
|
|
11
|
+
declare function Room$1(options: RoomOptions): (target: any) => void;
|
|
12
|
+
|
|
13
|
+
declare class MockPartySocket {
|
|
14
|
+
private events;
|
|
15
|
+
id: string;
|
|
16
|
+
addEventListener(event: any, cb: any): void;
|
|
17
|
+
removeEventListener(event: any, cb: any): void;
|
|
18
|
+
_trigger(event: any, data: any): void;
|
|
19
|
+
}
|
|
20
|
+
declare class MockStorage {
|
|
21
|
+
private storage;
|
|
22
|
+
get(key: string): Promise<any>;
|
|
23
|
+
put(key: string, value: any): Promise<void>;
|
|
24
|
+
list(): Promise<Map<string, any>>;
|
|
25
|
+
}
|
|
26
|
+
declare class MockPartyRoom {
|
|
27
|
+
id?: string;
|
|
28
|
+
private clients;
|
|
29
|
+
storage: MockStorage;
|
|
30
|
+
constructor(id?: string);
|
|
31
|
+
connection(client: any): void;
|
|
32
|
+
broadcast(data: any): void;
|
|
33
|
+
clear(): void;
|
|
34
|
+
}
|
|
35
|
+
declare class MockConnection {
|
|
36
|
+
state: any;
|
|
37
|
+
setState(value: any): void;
|
|
38
|
+
}
|
|
39
|
+
declare const ServerIo: typeof MockPartyRoom;
|
|
40
|
+
declare const ClientIo: typeof MockPartySocket;
|
|
5
41
|
|
|
6
42
|
type AssetFetcher = {
|
|
7
43
|
fetch(path: string): Promise<Response | null>;
|
|
@@ -173,17 +209,170 @@ type ServerOptions = {
|
|
|
173
209
|
hibernate?: boolean;
|
|
174
210
|
};
|
|
175
211
|
|
|
212
|
+
/**
|
|
213
|
+
* @class Server
|
|
214
|
+
* @implements {Party.Server}
|
|
215
|
+
* @description Represents a server that manages rooms and connections for a multiplayer game or application.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* import { Room, Server, ServerIo } from "@yourpackage/room";
|
|
220
|
+
*
|
|
221
|
+
* @Room({ path: "game" })
|
|
222
|
+
* class GameRoom {
|
|
223
|
+
* // Room implementation
|
|
224
|
+
* }
|
|
225
|
+
*
|
|
226
|
+
* class MyServer extends Server {
|
|
227
|
+
* rooms = [GameRoom];
|
|
228
|
+
* }
|
|
229
|
+
*
|
|
230
|
+
* const server = new MyServer(new ServerIo("game"));
|
|
231
|
+
* server.onStart();
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
176
234
|
declare class Server implements Server$1 {
|
|
177
235
|
readonly room: Room;
|
|
178
|
-
|
|
179
|
-
subRoom: {};
|
|
236
|
+
subRoom: any;
|
|
180
237
|
rooms: any[];
|
|
181
|
-
|
|
238
|
+
/**
|
|
239
|
+
* @constructor
|
|
240
|
+
* @param {Party.Room} room - The room object representing the current game or application instance.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const server = new MyServer(new ServerIo("game"));
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
182
247
|
constructor(room: Room);
|
|
248
|
+
/**
|
|
249
|
+
* @readonly
|
|
250
|
+
* @property {boolean} isHibernate - Indicates whether the server is in hibernate mode.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* if (!server.isHibernate) {
|
|
255
|
+
* console.log("Server is active");
|
|
256
|
+
* }
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
get isHibernate(): boolean;
|
|
260
|
+
/**
|
|
261
|
+
* @method onStart
|
|
262
|
+
* @async
|
|
263
|
+
* @description Initializes the server and creates the initial room if not in hibernate mode.
|
|
264
|
+
* @returns {Promise<void>}
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* async function initServer() {
|
|
269
|
+
* await server.onStart();
|
|
270
|
+
* console.log("Server started");
|
|
271
|
+
* }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
onStart(): Promise<void>;
|
|
275
|
+
/**
|
|
276
|
+
* @method createRoom
|
|
277
|
+
* @private
|
|
278
|
+
* @async
|
|
279
|
+
* @param {CreateRoomOptions} [options={}] - Options for creating the room.
|
|
280
|
+
* @returns {Promise<Object>} The created room instance.
|
|
281
|
+
* @throws {Error} If no matching room is found.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```typescript
|
|
285
|
+
* // This method is private and called internally
|
|
286
|
+
* async function internalCreateRoom() {
|
|
287
|
+
* const room = await this.createRoom({ getMemoryAll: true });
|
|
288
|
+
* console.log("Room created:", room);
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
private createRoom;
|
|
293
|
+
/**
|
|
294
|
+
* @method getSubRoom
|
|
295
|
+
* @private
|
|
296
|
+
* @async
|
|
297
|
+
* @param {Object} [options={}] - Options for getting the sub-room.
|
|
298
|
+
* @returns {Promise<Object>} The sub-room instance.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```typescript
|
|
302
|
+
* // This method is private and called internally
|
|
303
|
+
* async function internalGetSubRoom() {
|
|
304
|
+
* const subRoom = await this.getSubRoom();
|
|
305
|
+
* console.log("Sub-room retrieved:", subRoom);
|
|
306
|
+
* }
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
private getSubRoom;
|
|
310
|
+
/**
|
|
311
|
+
* @method getUsersProperty
|
|
312
|
+
* @private
|
|
313
|
+
* @param {Object} subRoom - The sub-room instance.
|
|
314
|
+
* @returns {Object|null} The users property of the sub-room, or null if not found.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```typescript
|
|
318
|
+
* // This method is private and called internally
|
|
319
|
+
* function internalGetUsers(subRoom) {
|
|
320
|
+
* const users = this.getUsersProperty(subRoom);
|
|
321
|
+
* console.log("Users:", users);
|
|
322
|
+
* }
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
183
325
|
private getUsersProperty;
|
|
326
|
+
/**
|
|
327
|
+
* @method onConnect
|
|
328
|
+
* @async
|
|
329
|
+
* @param {Party.Connection} conn - The connection object for the new user.
|
|
330
|
+
* @param {Party.ConnectionContext} ctx - The context of the connection.
|
|
331
|
+
* @description Handles a new user connection, creates a user object, and sends initial sync data.
|
|
332
|
+
* @returns {Promise<void>}
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```typescript
|
|
336
|
+
* server.onConnect = async (conn, ctx) => {
|
|
337
|
+
* await server.onConnect(conn, ctx);
|
|
338
|
+
* console.log("New user connected:", conn.id);
|
|
339
|
+
* };
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
184
342
|
onConnect(conn: Connection, ctx: ConnectionContext): Promise<void>;
|
|
343
|
+
/**
|
|
344
|
+
* @method onMessage
|
|
345
|
+
* @async
|
|
346
|
+
* @param {string} message - The message received from a user.
|
|
347
|
+
* @param {Party.Connection} sender - The connection object of the sender.
|
|
348
|
+
* @description Processes incoming messages and triggers corresponding actions in the sub-room.
|
|
349
|
+
* @returns {Promise<void>}
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```typescript
|
|
353
|
+
* server.onMessage = async (message, sender) => {
|
|
354
|
+
* await server.onMessage(message, sender);
|
|
355
|
+
* console.log("Message processed from:", sender.id);
|
|
356
|
+
* };
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
185
359
|
onMessage(message: string, sender: Connection): Promise<void>;
|
|
360
|
+
/**
|
|
361
|
+
* @method onClose
|
|
362
|
+
* @async
|
|
363
|
+
* @param {Party.Connection} conn - The connection object of the disconnecting user.
|
|
364
|
+
* @description Handles user disconnection, removing them from the room and triggering the onLeave event.
|
|
365
|
+
* @returns {Promise<void>}
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* server.onClose = async (conn) => {
|
|
370
|
+
* await server.onClose(conn);
|
|
371
|
+
* console.log("User disconnected:", conn.id);
|
|
372
|
+
* };
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
186
375
|
onClose(conn: Connection): Promise<void>;
|
|
187
376
|
}
|
|
188
377
|
|
|
189
|
-
export { Room$1 as Room, Server,
|
|
378
|
+
export { Action, ClientIo, MockConnection, Room$1 as Room, type RoomOptions, Server, ServerIo };
|