@unboxy/phaser-sdk 0.2.4 → 0.2.5
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/SDK-GUIDE.md +52 -1
- package/dist/core/Transport.d.ts +6 -0
- package/dist/core/Unboxy.d.ts +2 -0
- package/dist/core/Unboxy.js +6 -1
- package/dist/core/transports/PostMessageTransport.d.ts +1 -0
- package/dist/core/transports/PostMessageTransport.js +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2 -0
- package/dist/protocol.d.ts +16 -1
- package/dist/realtime/RealtimeModule.d.ts +30 -0
- package/dist/realtime/RealtimeModule.js +54 -0
- package/dist/realtime/UnboxyRoom.d.ts +36 -0
- package/dist/realtime/UnboxyRoom.js +52 -0
- package/package.json +9 -2
package/SDK-GUIDE.md
CHANGED
|
@@ -9,7 +9,7 @@ Reference for AI agents building games on the Unboxy platform. Tracks the **inst
|
|
|
9
9
|
- `Unboxy.init()` — platform services entry point
|
|
10
10
|
- `unboxy.saves` — **per-user** key-value store (only the owner sees/writes their data)
|
|
11
11
|
- `unboxy.gameData` — **per-game** key-value store (read by anyone; write requires auth)
|
|
12
|
-
-
|
|
12
|
+
- `unboxy.rooms` — **multiplayer rooms** (server-authoritative state sync, requires sign-in and host support)
|
|
13
13
|
|
|
14
14
|
## Platform services
|
|
15
15
|
|
|
@@ -183,6 +183,56 @@ Rule of thumb: if two different players would write to the same key, it's `gameD
|
|
|
183
183
|
- **Reads are cheap and public.** No auth needed — even logged-out players see `gameData`. Don't put anything secret here.
|
|
184
184
|
- **Trust is thin.** The backend stores what you write, verbatim. A player with devtools could submit any JSON. For casual games this is fine; for competitive stakes, a future typed leaderboards API with server-side validation is the right fix.
|
|
185
185
|
|
|
186
|
+
### Multiplayer rooms — `unboxy.rooms`
|
|
187
|
+
|
|
188
|
+
Realtime rooms backed by unboxy-realtime-service. The server is authoritative — clients send intents; the server updates state; every client receives the delta. This layer is for *live* shared play; persistent leaderboards/scores still belong in `gameData` or `saves`.
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
// Join or create a room scoped to this game. The SDK fetches a short-lived
|
|
192
|
+
// token from the host and forwards it; the iframe never handles credentials.
|
|
193
|
+
const room = await unboxy.rooms.joinOrCreate('lobby', {
|
|
194
|
+
displayName: unboxy.user?.name ?? 'guest',
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Read server state (Colyseus Schema proxy — access fields directly)
|
|
198
|
+
room.state; // e.g. { players: Map, gameId, ... } depending on room type
|
|
199
|
+
room.id; // room identifier
|
|
200
|
+
room.sessionId; // this connection's session id within the room
|
|
201
|
+
|
|
202
|
+
// React to server updates
|
|
203
|
+
const offState = room.onStateChange((state) => {
|
|
204
|
+
// re-render player list, scores, etc.
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Messages (server ↔ client, typed by name)
|
|
208
|
+
const offHit = room.on('hit', (payload) => { /* apply a hit effect */ });
|
|
209
|
+
room.send('move', { x: 120, y: 340 });
|
|
210
|
+
|
|
211
|
+
// Connection lifecycle
|
|
212
|
+
room.onLeave((code) => console.log('disconnected', code));
|
|
213
|
+
room.onError((code, message) => console.warn('room error', code, message));
|
|
214
|
+
|
|
215
|
+
// Clean up when the scene is destroyed
|
|
216
|
+
offState();
|
|
217
|
+
offHit();
|
|
218
|
+
await room.leave();
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
**Availability**
|
|
222
|
+
- Requires sign-in. Anonymous players get `RpcError('UNAUTHENTICATED')`.
|
|
223
|
+
- Requires a host with multiplayer enabled. Standalone games (no host) get `RpcError('REALTIME_UNAVAILABLE')` — guard with `if (unboxy.host === 'unboxy-home-ui')` if your game can run both hosted and standalone.
|
|
224
|
+
|
|
225
|
+
**Available room types (server-owned)**
|
|
226
|
+
- `lobby` — simple player list with `ready` flag and chat broadcast. Use for pre-match staging.
|
|
227
|
+
- (more coming: `TurnBasedRoom`, `RealtimeSyncRoom`)
|
|
228
|
+
|
|
229
|
+
**Rules of the road**
|
|
230
|
+
- The server is the source of truth. Never trust `send()` payloads coming from other clients — the server validates.
|
|
231
|
+
- Room state is *ephemeral*. When the last player leaves, it disposes. Persist anything you want to keep via `saves` or `gameData`.
|
|
232
|
+
- Keep messages small. State syncs are delta-compressed; raw `send()` payloads are not.
|
|
233
|
+
- Unsubscribe on scene shutdown. `onStateChange`, `on`, `onLeave`, `onError` all return an unsubscribe function — call them from `scene.events.once('shutdown', ...)`.
|
|
234
|
+
- One Colyseus client is kept internally; repeated `joinOrCreate` calls reuse it and mint fresh tokens each connection.
|
|
235
|
+
|
|
186
236
|
## Anti-patterns (don't do these)
|
|
187
237
|
|
|
188
238
|
- Do **not** call `Unboxy.init()` inside a scene. Initialize at module load in `main.ts` and export the promise.
|
|
@@ -195,6 +245,7 @@ Rule of thumb: if two different players would write to the same key, it's `gameD
|
|
|
195
245
|
|
|
196
246
|
## Changelog
|
|
197
247
|
|
|
248
|
+
- **0.2.5** — added `unboxy.rooms` module (server-authoritative multiplayer rooms backed by Colyseus on unboxy-realtime-service). Requires sign-in. Host must advertise `realtime` capability. Colyseus client loaded lazily — single-player games do not pay for the dependency.
|
|
198
249
|
- **0.2.4** — added `unboxy.gameData` module (game-scope key-value store for scoreboards, shared state)
|
|
199
250
|
- **0.2.3** — anonymous users now use localStorage even inside a host (was throwing UNAUTHENTICATED when calling `saves` in home-ui without login)
|
|
200
251
|
- **0.2.2** — added `SDK-GUIDE.md`
|
package/dist/core/Transport.d.ts
CHANGED
|
@@ -13,6 +13,12 @@ export interface Transport {
|
|
|
13
13
|
readonly kind: TransportKind;
|
|
14
14
|
readonly user: UnboxyUser | null;
|
|
15
15
|
readonly gameId: string;
|
|
16
|
+
/**
|
|
17
|
+
* Endpoint for unboxy-realtime-service (WebSocket URL). Set by the host
|
|
18
|
+
* during handshake when multiplayer is enabled. Absent in standalone mode —
|
|
19
|
+
* `unboxy.rooms.*` is unavailable in that case.
|
|
20
|
+
*/
|
|
21
|
+
readonly realtimeUrl?: string;
|
|
16
22
|
call<T = unknown>(method: string, params?: unknown): Promise<T>;
|
|
17
23
|
}
|
|
18
24
|
export type TransportKind = 'unboxy-home-ui' | 'standalone' | 'discord';
|
package/dist/core/Unboxy.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TransportKind } from './Transport.js';
|
|
2
2
|
import { SavesModule } from '../saves/SavesModule.js';
|
|
3
3
|
import { GameDataModule } from '../gamedata/GameDataModule.js';
|
|
4
|
+
import { RealtimeModule } from '../realtime/RealtimeModule.js';
|
|
4
5
|
import type { UnboxyUser } from '../protocol.js';
|
|
5
6
|
export interface UnboxyInitOptions {
|
|
6
7
|
/** Handshake timeout in ms when running inside a host. Default 2000. */
|
|
@@ -22,6 +23,7 @@ export declare class Unboxy {
|
|
|
22
23
|
private transport;
|
|
23
24
|
readonly saves: SavesModule;
|
|
24
25
|
readonly gameData: GameDataModule;
|
|
26
|
+
readonly rooms: RealtimeModule;
|
|
25
27
|
private constructor();
|
|
26
28
|
/** The current authenticated user, or `null` if anonymous / standalone. */
|
|
27
29
|
get user(): UnboxyUser | null;
|
package/dist/core/Unboxy.js
CHANGED
|
@@ -2,8 +2,9 @@ import { PostMessageTransport } from './transports/PostMessageTransport.js';
|
|
|
2
2
|
import { LocalStorageTransport } from './transports/LocalStorageTransport.js';
|
|
3
3
|
import { SavesModule } from '../saves/SavesModule.js';
|
|
4
4
|
import { GameDataModule } from '../gamedata/GameDataModule.js';
|
|
5
|
+
import { RealtimeModule } from '../realtime/RealtimeModule.js';
|
|
5
6
|
// Kept in sync with package.json on each publish.
|
|
6
|
-
const SDK_VERSION = '0.2.
|
|
7
|
+
const SDK_VERSION = '0.2.5';
|
|
7
8
|
/**
|
|
8
9
|
* Unboxy platform services bound to the current (game, user).
|
|
9
10
|
*
|
|
@@ -27,6 +28,10 @@ export class Unboxy {
|
|
|
27
28
|
// fallback (standalone mode) that also works — the module speaks the
|
|
28
29
|
// same RPC interface.
|
|
29
30
|
this.gameData = new GameDataModule(transport);
|
|
31
|
+
// Multiplayer requires a realtime endpoint from the host. When absent
|
|
32
|
+
// (standalone or a host without multiplayer), methods throw a clear
|
|
33
|
+
// REALTIME_UNAVAILABLE error rather than silently misbehaving.
|
|
34
|
+
this.rooms = new RealtimeModule(transport, transport.realtimeUrl);
|
|
30
35
|
}
|
|
31
36
|
/** The current authenticated user, or `null` if anonymous / standalone. */
|
|
32
37
|
get user() { return this.transport.user; }
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,10 @@ export { Unboxy } from './core/Unboxy.js';
|
|
|
7
7
|
export type { UnboxyInitOptions } from './core/Unboxy.js';
|
|
8
8
|
export { SavesModule } from './saves/SavesModule.js';
|
|
9
9
|
export { GameDataModule } from './gamedata/GameDataModule.js';
|
|
10
|
+
export { RealtimeModule } from './realtime/RealtimeModule.js';
|
|
11
|
+
export type { JoinOptions } from './realtime/RealtimeModule.js';
|
|
12
|
+
export { UnboxyRoom } from './realtime/UnboxyRoom.js';
|
|
10
13
|
export { RpcError } from './core/Transport.js';
|
|
11
14
|
export type { Transport, TransportKind } from './core/Transport.js';
|
|
12
15
|
export type { UnboxyUser } from './protocol.js';
|
|
13
|
-
export { PROTOCOL_VERSION, type HelloMessage, type InitMessage, type RpcRequestMessage, type RpcResultOk, type RpcResultError, type HostToSdkMessage, type SdkToHostMessage, type RpcErrorPayload, type RpcMethod, type SavesGetParams, type SavesGetResult, type SavesSetParams, type SavesSetResult, type SavesDeleteParams, type SavesDeleteResult, type SavesListResult, type GameDataGetParams, type GameDataGetResult, type GameDataSetParams, type GameDataSetResult, type GameDataDeleteParams, type GameDataDeleteResult, type GameDataListResult, } from './protocol.js';
|
|
16
|
+
export { PROTOCOL_VERSION, type HelloMessage, type InitMessage, type RpcRequestMessage, type RpcResultOk, type RpcResultError, type HostToSdkMessage, type SdkToHostMessage, type RpcErrorPayload, type RpcMethod, type SavesGetParams, type SavesGetResult, type SavesSetParams, type SavesSetResult, type SavesDeleteParams, type SavesDeleteResult, type SavesListResult, type GameDataGetParams, type GameDataGetResult, type GameDataSetParams, type GameDataSetResult, type GameDataDeleteParams, type GameDataDeleteResult, type GameDataListResult, type RealtimeGetTokenParams, type RealtimeGetTokenResult, } from './protocol.js';
|
package/dist/index.js
CHANGED
|
@@ -9,5 +9,7 @@ export { setupRecordingListener } from './recording/RecordingManager.js';
|
|
|
9
9
|
export { Unboxy } from './core/Unboxy.js';
|
|
10
10
|
export { SavesModule } from './saves/SavesModule.js';
|
|
11
11
|
export { GameDataModule } from './gamedata/GameDataModule.js';
|
|
12
|
+
export { RealtimeModule } from './realtime/RealtimeModule.js';
|
|
13
|
+
export { UnboxyRoom } from './realtime/UnboxyRoom.js';
|
|
12
14
|
export { RpcError } from './core/Transport.js';
|
|
13
15
|
export { PROTOCOL_VERSION, } from './protocol.js';
|
package/dist/protocol.d.ts
CHANGED
|
@@ -22,6 +22,12 @@ export interface InitMessage {
|
|
|
22
22
|
gameId: string;
|
|
23
23
|
user: UnboxyUser | null;
|
|
24
24
|
capabilities: string[];
|
|
25
|
+
/**
|
|
26
|
+
* WebSocket endpoint for unboxy-realtime-service. Present only when the host
|
|
27
|
+
* has multiplayer configured (capabilities includes 'realtime'). SDK connects
|
|
28
|
+
* here after fetching a JWT via the 'realtime.getToken' RPC.
|
|
29
|
+
*/
|
|
30
|
+
realtimeUrl?: string;
|
|
25
31
|
}
|
|
26
32
|
export interface RpcResultOk {
|
|
27
33
|
type: 'unboxy:rpc.result';
|
|
@@ -40,7 +46,7 @@ export interface RpcResultError {
|
|
|
40
46
|
error: RpcErrorPayload;
|
|
41
47
|
}
|
|
42
48
|
export type HostToSdkMessage = InitMessage | RpcResultOk | RpcResultError;
|
|
43
|
-
export type RpcMethod = 'saves.get' | 'saves.set' | 'saves.delete' | 'saves.list' | 'gameData.get' | 'gameData.set' | 'gameData.delete' | 'gameData.list';
|
|
49
|
+
export type RpcMethod = 'saves.get' | 'saves.set' | 'saves.delete' | 'saves.list' | 'gameData.get' | 'gameData.set' | 'gameData.delete' | 'gameData.list' | 'realtime.getToken';
|
|
44
50
|
export interface SavesGetParams {
|
|
45
51
|
key: string;
|
|
46
52
|
}
|
|
@@ -89,3 +95,12 @@ export type GameDataDeleteResult = {
|
|
|
89
95
|
export interface GameDataListResult {
|
|
90
96
|
keys: string[];
|
|
91
97
|
}
|
|
98
|
+
export interface RealtimeGetTokenParams {
|
|
99
|
+
/** Opaque, forwarded to server-side auth (future use). */
|
|
100
|
+
purpose?: string;
|
|
101
|
+
}
|
|
102
|
+
export interface RealtimeGetTokenResult {
|
|
103
|
+
token: string;
|
|
104
|
+
/** Epoch millis when the token expires. */
|
|
105
|
+
expiresAt: number;
|
|
106
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Transport } from '../core/Transport.js';
|
|
2
|
+
import { UnboxyRoom } from './UnboxyRoom.js';
|
|
3
|
+
export interface JoinOptions extends Record<string, unknown> {
|
|
4
|
+
/** Optional display name passed to the server-side room on join. */
|
|
5
|
+
displayName?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Multiplayer rooms backed by unboxy-realtime-service (Colyseus under the hood).
|
|
9
|
+
*
|
|
10
|
+
* Availability: only when the host provided a `realtimeUrl` during the
|
|
11
|
+
* handshake (i.e. connected to unboxy-home-ui with multiplayer enabled).
|
|
12
|
+
* Unavailable in standalone/anonymous mode — methods throw RpcError
|
|
13
|
+
* 'REALTIME_UNAVAILABLE'.
|
|
14
|
+
*
|
|
15
|
+
* Tokens are fetched per connection attempt via the `realtime.getToken`
|
|
16
|
+
* RPC — the iframe never holds a long-lived credential.
|
|
17
|
+
*/
|
|
18
|
+
export declare class RealtimeModule {
|
|
19
|
+
private readonly transport;
|
|
20
|
+
private readonly realtimeUrl;
|
|
21
|
+
private client;
|
|
22
|
+
constructor(transport: Transport, realtimeUrl: string | undefined);
|
|
23
|
+
/** `true` when a realtime endpoint was provided by the host. */
|
|
24
|
+
get available(): boolean;
|
|
25
|
+
joinOrCreate<State = unknown>(roomType: string, options?: JoinOptions): Promise<UnboxyRoom<State>>;
|
|
26
|
+
create<State = unknown>(roomType: string, options?: JoinOptions): Promise<UnboxyRoom<State>>;
|
|
27
|
+
join<State = unknown>(roomType: string, options?: JoinOptions): Promise<UnboxyRoom<State>>;
|
|
28
|
+
joinById<State = unknown>(roomId: string, options?: JoinOptions): Promise<UnboxyRoom<State>>;
|
|
29
|
+
private connect;
|
|
30
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { RpcError } from '../core/Transport.js';
|
|
2
|
+
import { UnboxyRoom } from './UnboxyRoom.js';
|
|
3
|
+
/**
|
|
4
|
+
* Multiplayer rooms backed by unboxy-realtime-service (Colyseus under the hood).
|
|
5
|
+
*
|
|
6
|
+
* Availability: only when the host provided a `realtimeUrl` during the
|
|
7
|
+
* handshake (i.e. connected to unboxy-home-ui with multiplayer enabled).
|
|
8
|
+
* Unavailable in standalone/anonymous mode — methods throw RpcError
|
|
9
|
+
* 'REALTIME_UNAVAILABLE'.
|
|
10
|
+
*
|
|
11
|
+
* Tokens are fetched per connection attempt via the `realtime.getToken`
|
|
12
|
+
* RPC — the iframe never holds a long-lived credential.
|
|
13
|
+
*/
|
|
14
|
+
export class RealtimeModule {
|
|
15
|
+
constructor(transport, realtimeUrl) {
|
|
16
|
+
this.transport = transport;
|
|
17
|
+
this.realtimeUrl = realtimeUrl;
|
|
18
|
+
this.client = null;
|
|
19
|
+
}
|
|
20
|
+
/** `true` when a realtime endpoint was provided by the host. */
|
|
21
|
+
get available() {
|
|
22
|
+
return typeof this.realtimeUrl === 'string' && this.realtimeUrl.length > 0;
|
|
23
|
+
}
|
|
24
|
+
async joinOrCreate(roomType, options = {}) {
|
|
25
|
+
return this.connect(roomType, options, 'joinOrCreate');
|
|
26
|
+
}
|
|
27
|
+
async create(roomType, options = {}) {
|
|
28
|
+
return this.connect(roomType, options, 'create');
|
|
29
|
+
}
|
|
30
|
+
async join(roomType, options = {}) {
|
|
31
|
+
return this.connect(roomType, options, 'join');
|
|
32
|
+
}
|
|
33
|
+
async joinById(roomId, options = {}) {
|
|
34
|
+
return this.connect(roomId, options, 'joinById');
|
|
35
|
+
}
|
|
36
|
+
async connect(nameOrId, options, method) {
|
|
37
|
+
if (!this.available) {
|
|
38
|
+
throw new RpcError('REALTIME_UNAVAILABLE', 'Multiplayer is not available for this host. Unboxy.rooms requires running inside unboxy-home-ui with realtime enabled.');
|
|
39
|
+
}
|
|
40
|
+
if (!this.client) {
|
|
41
|
+
// Dynamic import so single-player games do not pull Colyseus into their
|
|
42
|
+
// bundle. Vite tree-shakes this path when `rooms` is never referenced.
|
|
43
|
+
const { Client } = await import('colyseus.js');
|
|
44
|
+
this.client = new Client(this.realtimeUrl);
|
|
45
|
+
}
|
|
46
|
+
const { token } = await this.transport.call('realtime.getToken', {});
|
|
47
|
+
// Inject gameId from the transport so the server-side gameId check sees
|
|
48
|
+
// a value consistent with the JWT — games never pass this directly.
|
|
49
|
+
const merged = { ...options, gameId: this.transport.gameId };
|
|
50
|
+
this.client.auth.token = token;
|
|
51
|
+
const room = await this.client[method](nameOrId, merged);
|
|
52
|
+
return new UnboxyRoom(room);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Room } from 'colyseus.js';
|
|
2
|
+
type Unsubscribe = () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Unboxy-flavored wrapper around a Colyseus Room. Exposes only the surface
|
|
5
|
+
* documented in SDK-GUIDE.md so games are portable to a different realtime
|
|
6
|
+
* backend should we migrate away from Colyseus.
|
|
7
|
+
*/
|
|
8
|
+
export declare class UnboxyRoom<State = unknown> {
|
|
9
|
+
private readonly room;
|
|
10
|
+
constructor(room: Room<State>);
|
|
11
|
+
get id(): string;
|
|
12
|
+
get name(): string;
|
|
13
|
+
get sessionId(): string;
|
|
14
|
+
/**
|
|
15
|
+
* Current server-authoritative state. Proxied from Colyseus Schema — read
|
|
16
|
+
* values directly. Mutations do not propagate; only server-side handlers
|
|
17
|
+
* may change state.
|
|
18
|
+
*/
|
|
19
|
+
get state(): State;
|
|
20
|
+
/** Send a typed message to the server. */
|
|
21
|
+
send(type: string, payload?: unknown): void;
|
|
22
|
+
/** Register a handler for a server-sent message type. Returns unsubscribe. */
|
|
23
|
+
on(type: string, handler: (payload: unknown) => void): Unsubscribe;
|
|
24
|
+
/** Fires whenever the server-authoritative state changes. */
|
|
25
|
+
onStateChange(handler: (state: State) => void): Unsubscribe;
|
|
26
|
+
/**
|
|
27
|
+
* Fires when the connection closes (kick, server shutdown, network drop).
|
|
28
|
+
* `code` follows WebSocket close codes plus Colyseus-specific ones.
|
|
29
|
+
*/
|
|
30
|
+
onLeave(handler: (code: number) => void): Unsubscribe;
|
|
31
|
+
/** Fires when the server reports an error for this room. */
|
|
32
|
+
onError(handler: (code: number, message?: string) => void): Unsubscribe;
|
|
33
|
+
/** Disconnect from the room. Resolves with the close code. */
|
|
34
|
+
leave(consented?: boolean): Promise<number>;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unboxy-flavored wrapper around a Colyseus Room. Exposes only the surface
|
|
3
|
+
* documented in SDK-GUIDE.md so games are portable to a different realtime
|
|
4
|
+
* backend should we migrate away from Colyseus.
|
|
5
|
+
*/
|
|
6
|
+
export class UnboxyRoom {
|
|
7
|
+
constructor(room) {
|
|
8
|
+
this.room = room;
|
|
9
|
+
}
|
|
10
|
+
get id() { return this.room.roomId; }
|
|
11
|
+
get name() { return this.room.name; }
|
|
12
|
+
get sessionId() { return this.room.sessionId; }
|
|
13
|
+
/**
|
|
14
|
+
* Current server-authoritative state. Proxied from Colyseus Schema — read
|
|
15
|
+
* values directly. Mutations do not propagate; only server-side handlers
|
|
16
|
+
* may change state.
|
|
17
|
+
*/
|
|
18
|
+
get state() { return this.room.state; }
|
|
19
|
+
/** Send a typed message to the server. */
|
|
20
|
+
send(type, payload) {
|
|
21
|
+
this.room.send(type, payload);
|
|
22
|
+
}
|
|
23
|
+
/** Register a handler for a server-sent message type. Returns unsubscribe. */
|
|
24
|
+
on(type, handler) {
|
|
25
|
+
return this.room.onMessage(type, handler);
|
|
26
|
+
}
|
|
27
|
+
/** Fires whenever the server-authoritative state changes. */
|
|
28
|
+
onStateChange(handler) {
|
|
29
|
+
const cb = () => handler(this.room.state);
|
|
30
|
+
this.room.onStateChange(cb);
|
|
31
|
+
return () => this.room.onStateChange.remove(cb);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Fires when the connection closes (kick, server shutdown, network drop).
|
|
35
|
+
* `code` follows WebSocket close codes plus Colyseus-specific ones.
|
|
36
|
+
*/
|
|
37
|
+
onLeave(handler) {
|
|
38
|
+
const cb = (code) => handler(code);
|
|
39
|
+
this.room.onLeave(cb);
|
|
40
|
+
return () => this.room.onLeave.remove(cb);
|
|
41
|
+
}
|
|
42
|
+
/** Fires when the server reports an error for this room. */
|
|
43
|
+
onError(handler) {
|
|
44
|
+
const cb = (code, message) => handler(code, message);
|
|
45
|
+
this.room.onError(cb);
|
|
46
|
+
return () => this.room.onError.remove(cb);
|
|
47
|
+
}
|
|
48
|
+
/** Disconnect from the room. Resolves with the close code. */
|
|
49
|
+
async leave(consented = true) {
|
|
50
|
+
return this.room.leave(consented);
|
|
51
|
+
}
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unboxy/phaser-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Unboxy Phaser 3 SDK — game infrastructure for the Unboxy platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
-
"files": [
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"SDK-GUIDE.md"
|
|
10
|
+
],
|
|
8
11
|
"scripts": {
|
|
9
12
|
"build": "tsc",
|
|
10
13
|
"prepublishOnly": "npm run build"
|
|
11
14
|
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"colyseus.js": "^0.16.0"
|
|
17
|
+
},
|
|
12
18
|
"peerDependencies": {
|
|
13
19
|
"phaser": "^3.60.0"
|
|
14
20
|
},
|
|
15
21
|
"devDependencies": {
|
|
22
|
+
"jose": "^6.2.2",
|
|
16
23
|
"phaser": "^3.80.0",
|
|
17
24
|
"typescript": "^5.5.0"
|
|
18
25
|
},
|