@watchtower-sdk/core 0.2.1 → 0.2.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.mts CHANGED
@@ -283,6 +283,12 @@ declare class Sync<T extends Record<string, unknown>> {
283
283
  * Leave the current room
284
284
  */
285
285
  leave(): Promise<void>;
286
+ /**
287
+ * Send a one-off message to all players in the room
288
+ *
289
+ * @param data - Any JSON-serializable data
290
+ */
291
+ broadcast(data: unknown): void;
286
292
  /**
287
293
  * Create a new room and join it
288
294
  *
@@ -297,7 +303,7 @@ declare class Sync<T extends Record<string, unknown>> {
297
303
  /**
298
304
  * Subscribe to sync events
299
305
  */
300
- on(event: 'join' | 'leave' | 'error' | 'connected' | 'disconnected', callback: Function): void;
306
+ on(event: 'join' | 'leave' | 'error' | 'connected' | 'disconnected' | 'message', callback: Function): void;
301
307
  /**
302
308
  * Unsubscribe from sync events
303
309
  */
package/dist/index.d.ts CHANGED
@@ -283,6 +283,12 @@ declare class Sync<T extends Record<string, unknown>> {
283
283
  * Leave the current room
284
284
  */
285
285
  leave(): Promise<void>;
286
+ /**
287
+ * Send a one-off message to all players in the room
288
+ *
289
+ * @param data - Any JSON-serializable data
290
+ */
291
+ broadcast(data: unknown): void;
286
292
  /**
287
293
  * Create a new room and join it
288
294
  *
@@ -297,7 +303,7 @@ declare class Sync<T extends Record<string, unknown>> {
297
303
  /**
298
304
  * Subscribe to sync events
299
305
  */
300
- on(event: 'join' | 'leave' | 'error' | 'connected' | 'disconnected', callback: Function): void;
306
+ on(event: 'join' | 'leave' | 'error' | 'connected' | 'disconnected' | 'message', callback: Function): void;
301
307
  /**
302
308
  * Unsubscribe from sync events
303
309
  */
package/dist/index.js CHANGED
@@ -333,6 +333,16 @@ var Sync = class {
333
333
  this.clearRemotePlayers();
334
334
  this._roomId = null;
335
335
  }
336
+ /**
337
+ * Send a one-off message to all players in the room
338
+ *
339
+ * @param data - Any JSON-serializable data
340
+ */
341
+ broadcast(data) {
342
+ if (this.ws?.readyState === WebSocket.OPEN) {
343
+ this.ws.send(JSON.stringify({ type: "broadcast", data }));
344
+ }
345
+ }
336
346
  /**
337
347
  * Create a new room and join it
338
348
  *
@@ -440,6 +450,9 @@ var Sync = class {
440
450
  this.removePlayer(data.playerId);
441
451
  this.emit("leave", data.playerId);
442
452
  break;
453
+ case "message":
454
+ this.emit("message", data.from, data.data);
455
+ break;
443
456
  }
444
457
  }
445
458
  applyFullState(fullState) {
package/dist/index.mjs CHANGED
@@ -306,6 +306,16 @@ var Sync = class {
306
306
  this.clearRemotePlayers();
307
307
  this._roomId = null;
308
308
  }
309
+ /**
310
+ * Send a one-off message to all players in the room
311
+ *
312
+ * @param data - Any JSON-serializable data
313
+ */
314
+ broadcast(data) {
315
+ if (this.ws?.readyState === WebSocket.OPEN) {
316
+ this.ws.send(JSON.stringify({ type: "broadcast", data }));
317
+ }
318
+ }
309
319
  /**
310
320
  * Create a new room and join it
311
321
  *
@@ -413,6 +423,9 @@ var Sync = class {
413
423
  this.removePlayer(data.playerId);
414
424
  this.emit("leave", data.playerId);
415
425
  break;
426
+ case "message":
427
+ this.emit("message", data.from, data.data);
428
+ break;
416
429
  }
417
430
  }
418
431
  applyFullState(fullState) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtower-sdk/core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Simple game backend SDK - saves, multiplayer rooms, and more",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",