playsocketjs 2.3.0 → 2.4.1
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 +1 -0
- package/dist/playsocket-client.js +1 -0
- package/dist/playsocket-server.js +17 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -219,6 +219,7 @@ Creates a new PlaySocket Server instance with configuration options.
|
|
|
219
219
|
- `clientRegistrationRequested`: Client requests to register (returns the client's ID `string`, customData `object`) – return `false` or a rejection reason `string` to block the registration
|
|
220
220
|
- `clientDisconnected`: Client disconnected from the server (returns the client's ID `string`)
|
|
221
221
|
- `clientJoinedRoom`: Client joined a room – note that clients can only leave by disconnecting (returns the client's ID `string`, room ID `string`)
|
|
222
|
+
- `clientJoinRequested`: Client requests to join a room (returns the client's ID `string`, room ID `string`) – return `false` or a rejection reason `string` to block them from joining
|
|
222
223
|
- `roomCreated`: Client created a room (returns room ID `string`)
|
|
223
224
|
- `roomDestroyed`: Room was destroyed, this happens when all participants leave (returns room ID `string`)
|
|
224
225
|
- `roomCreationRequested`: Room creation requested by client (returns `object` containing the client's ID `string`, room ID `string` and the initialStorage `object`) – if you return an `object` in the callback, it will take that as the initial storage instead. If you return `false`, the creation will be denied
|
|
@@ -2505,6 +2505,7 @@ class PlaySocket {
|
|
|
2505
2505
|
* @param {*} updateValue - New value for update-matching
|
|
2506
2506
|
*/
|
|
2507
2507
|
updateStorage(key, type, value, updateValue) {
|
|
2508
|
+
if (!this.#inRoom) return this.#triggerEvent("error", "Cannot update storage when not in a room.");
|
|
2508
2509
|
if (this.#debug) console.log(LOG_PREFIX + `Property update for key ${key}, operation ${type}, value ${value} and updateValue ${updateValue}.`);
|
|
2509
2510
|
const propUpdate = this.#crdtManager.updateProperty(key, type, value, updateValue);
|
|
2510
2511
|
this.#sendToServer({
|
|
@@ -2351,7 +2351,7 @@ function requireServer () {
|
|
|
2351
2351
|
if (data.sessionToken !== this.#clientTokens.get(data.id)) {
|
|
2352
2352
|
ws.send(encode({
|
|
2353
2353
|
type: 'reconnection_failed',
|
|
2354
|
-
reason: "Session token does not match"
|
|
2354
|
+
reason: "Session token does not match."
|
|
2355
2355
|
}), { binary: true });
|
|
2356
2356
|
return;
|
|
2357
2357
|
}
|
|
@@ -2378,7 +2378,7 @@ function requireServer () {
|
|
|
2378
2378
|
|
|
2379
2379
|
ws.send(encode({ type: 'reconnected', roomData }), { binary: true });
|
|
2380
2380
|
} else {
|
|
2381
|
-
ws.send(encode({ type: 'reconnection_failed', reason: "Client unknown to server" }), { binary: true });
|
|
2381
|
+
ws.send(encode({ type: 'reconnection_failed', reason: "Client unknown to server." }), { binary: true });
|
|
2382
2382
|
}
|
|
2383
2383
|
break;
|
|
2384
2384
|
|
|
@@ -2389,7 +2389,7 @@ function requireServer () {
|
|
|
2389
2389
|
if (this.#clientRooms.get(ws.clientId) || this.#rooms[newRoomId]) {
|
|
2390
2390
|
ws.send(encode({
|
|
2391
2391
|
type: 'room_creation_failed',
|
|
2392
|
-
reason: this.#clientRooms.get(ws.clientId) ? 'Already in a room' : 'Room ID is taken'
|
|
2392
|
+
reason: this.#clientRooms.get(ws.clientId) ? 'Already in a room.' : 'Room ID is taken.'
|
|
2393
2393
|
}), { binary: true });
|
|
2394
2394
|
return;
|
|
2395
2395
|
}
|
|
@@ -2440,17 +2440,17 @@ function requireServer () {
|
|
|
2440
2440
|
const roomId = data.roomId;
|
|
2441
2441
|
const room = this.#rooms[roomId];
|
|
2442
2442
|
|
|
2443
|
-
|
|
2444
|
-
(
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2443
|
+
const rejectJoin = (reason) => {
|
|
2444
|
+
ws.send(encode({ type: 'join_rejected', reason }), { binary: true });
|
|
2445
|
+
};
|
|
2446
|
+
|
|
2447
|
+
// Event callback
|
|
2448
|
+
const joinAllowed = await this.#triggerEvent("clientJoinRequested", ws.clientId, roomId);
|
|
2449
|
+
if (joinAllowed === false || typeof joinAllowed === 'string') return rejectJoin(typeof joinAllowed === 'string' ? joinAllowed : 'Denied.');
|
|
2450
|
+
|
|
2451
|
+
if (!room) return rejectJoin("Room not found.");
|
|
2452
|
+
if (this.#clientRooms.get(ws.clientId)) return rejectJoin("Already in a room.");
|
|
2453
|
+
if (room.participants.length >= room.maxSize) return rejectJoin("Room full.");
|
|
2454
2454
|
|
|
2455
2455
|
room.participants.push(ws.clientId);
|
|
2456
2456
|
this.#clientRooms.set(ws.clientId, roomId);
|
|
@@ -2628,7 +2628,7 @@ function requireServer () {
|
|
|
2628
2628
|
#disconnectClient(ws) {
|
|
2629
2629
|
this.#pendingDisconnects.delete(ws.clientId);
|
|
2630
2630
|
this.#clientTokens.delete(ws.clientId);
|
|
2631
|
-
|
|
2631
|
+
let roomId = this.#clientRooms.get(ws.clientId);
|
|
2632
2632
|
const room = this.#rooms[roomId];
|
|
2633
2633
|
|
|
2634
2634
|
if (room) {
|
|
@@ -2640,6 +2640,7 @@ function requireServer () {
|
|
|
2640
2640
|
this.#roomVersions.delete(roomId); // Delete room version
|
|
2641
2641
|
this.#triggerEvent("roomDestroyed", roomId);
|
|
2642
2642
|
if (this.#debug) console.log("Deleted room with id " + roomId + ".");
|
|
2643
|
+
roomId = null; // If room was destroyed, set roomId to null
|
|
2643
2644
|
} else {
|
|
2644
2645
|
// Notify remaining participants
|
|
2645
2646
|
room.participants.forEach(p => {
|
|
@@ -2662,7 +2663,7 @@ function requireServer () {
|
|
|
2662
2663
|
* @param {Function} callback - Callback function
|
|
2663
2664
|
*/
|
|
2664
2665
|
onEvent(event, callback) {
|
|
2665
|
-
const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "roomCreated", "roomCreationRequested", "requestReceived", "storageUpdated", "storageUpdateRequested", "roomDestroyed"];
|
|
2666
|
+
const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "clientJoinRequested", "roomCreated", "roomCreationRequested", "requestReceived", "storageUpdated", "storageUpdateRequested", "roomDestroyed"];
|
|
2666
2667
|
if (!validEvents.includes(event)) return console.warn(`Invalid PlaySocket event type "${event}"`);
|
|
2667
2668
|
if (!this.#callbacks.has(event)) this.#callbacks.set(event, []);
|
|
2668
2669
|
this.#callbacks.get(event).push(callback);
|