angelia.io 4.0.90 → 4.0.91

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "4.0.90",
3
+ "version": "4.0.91",
4
4
  "description": "WebSockets Server and Client API for node.js and the browser, with rooms support.",
5
5
  "author": "@titoBouzout @boredofnames",
6
6
  "license": "GPL",
package/readme.md CHANGED
@@ -205,13 +205,15 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
205
205
 
206
206
  ## Rooms
207
207
 
208
+ ### Via a `Rooms` list
209
+
208
210
  1. Create a room class that extends `Room`
209
211
  2. Create a room list that will allow joining sockets
210
212
  3. Join a socket in a room, a socket may join multiple rooms. Rooms
211
213
  are created when the `id` of the room doesn't exists, and deleted
212
214
  when there are no sockets in the room and the room doesn't have the
213
215
  flag `persistent`
214
- 4. a socket will leave automatically all rooms on disconnection
216
+ 4. A socket will leave automatically all rooms on disconnection
215
217
  5. `socket.rooms` is a set with all the rooms the socket joined
216
218
  6. `id` is assigned to the room on creation
217
219
 
@@ -219,7 +221,7 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
219
221
  import { Room, Rooms } from 'angelia.io/server'
220
222
 
221
223
  class GameRoom extends Room {
222
- persistent = true
224
+ persistent = false
223
225
 
224
226
  onCreate(socket) {
225
227
  console.log('creating room', this.id, socket)
@@ -240,6 +242,7 @@ class GameRoom extends Room {
240
242
  }
241
243
  }
242
244
 
245
+ // games is a room list
243
246
  const games = new Rooms(GameRoom)
244
247
 
245
248
  class Connection {
@@ -259,7 +262,62 @@ Server.on(Connection)
259
262
  Server.listen()
260
263
  ```
261
264
 
262
- ### `Room` Class
265
+ #### `Rooms` Class
266
+
267
+ Iterating returns the rooms
268
+
269
+ | signature | kind | description |
270
+ | ------------------------------------- | -------- | --------------------------------------------- |
271
+ | `get(id)` | method | gets room with such an `id` |
272
+ | `join(socket, [id])` | method | adds socket to room `id` |
273
+ | `leave(socket, [id])` | method | removes socket from room `id` |
274
+ | `connections` | number | number of sockets in this room list |
275
+ | `sockets` | Iterator | sockets in this room list |
276
+ | `emit(key, [value])` | method | emits to all sockets in this room list |
277
+ | `once(key, [value])` | method | emits once to all sockets in this room list |
278
+ | `broadcast(socket, key, [value])` | method | emits to other sockets in this room list |
279
+ | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in this room list |
280
+ | `map(fn)` | method | returns mapped array |
281
+ | `filter(fn)` | method | returns mapped filtered array |
282
+
283
+ ### Via a `Room`
284
+
285
+ 1. Create a room instance that extends `Room`
286
+ 2. Join a socket in a room, a socket may join multiple rooms.
287
+ 3. A socket will leave automatically all rooms on disconnection
288
+ 4. `socket.rooms` is a set with all the rooms the socket joined
289
+ 5. Keep in mind that `onCreate` and `onDelete` are not dispatched when
290
+ the room is manged by yourself. The construtor could be used for
291
+ simulating `onCreate` and `onDelete` only you will know when to
292
+ dispose such a variable.
293
+
294
+ ```js
295
+ import { Room } from 'angelia.io/server'
296
+
297
+ class Lobby extends Room {
298
+ persistent = true
299
+ id = 'lobby'
300
+ onJoin(socket) {
301
+ console.log('socket joined room', this.id, socket)
302
+ }
303
+ }
304
+
305
+ // lobby is just 1 room
306
+ const lobby = new Lobby()
307
+
308
+ class Connection {
309
+ connect(socket) {
310
+ lobby.join(socket)
311
+
312
+ lobby.leave(socket)
313
+ }
314
+ }
315
+
316
+ Server.on(Connection)
317
+ Server.listen()
318
+ ```
319
+
320
+ #### `Room` Class
263
321
 
264
322
  Iterating returns the sockets
265
323
 
@@ -277,26 +335,10 @@ Iterating returns the sockets
277
335
  | `broadcast(socket, key, [value])` | method | emits to other sockets in the room |
278
336
  | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
279
337
  | `id` | any | the room id |
338
+ | `join(socket)` | method | adds a socket to the room |
339
+ | `leave(socket)` | method | remves a socket from the room |
280
340
 
281
- ### `Rooms` Class
282
-
283
- Iterating returns the rooms
284
-
285
- | signature | kind | description |
286
- | ------------------------------------- | -------- | --------------------------------------------- |
287
- | `get(id)` | method | gets room with such an `id` |
288
- | `join(socket, [id])` | method | adds socket to room `id` |
289
- | `leave(socket, [id])` | method | removes socket from room `id` |
290
- | `connections` | number | number of sockets in this room list |
291
- | `sockets` | Iterator | sockets in this room list |
292
- | `emit(key, [value])` | method | emits to all sockets in this room list |
293
- | `once(key, [value])` | method | emits once to all sockets in this room list |
294
- | `broadcast(socket, key, [value])` | method | emits to other sockets in this room list |
295
- | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in this room list |
296
- | `map(fn)` | method | returns mapped array |
297
- | `filter(fn)` | method | returns mapped filtered array |
298
-
299
- ### `sockets` Object
341
+ ### `sockets` Object in `Room` and `Rooms`
300
342
 
301
343
  Like `Set` but with two aditional methods
302
344
 
package/server/Room.js CHANGED
@@ -1,5 +1,3 @@
1
- import { join, leave } from './constants.js'
2
-
3
1
  import { Emitter } from './Emitter.js'
4
2
 
5
3
  export class Room extends Emitter {
@@ -16,7 +14,7 @@ export class Room extends Emitter {
16
14
  onLeave(socket) {}
17
15
 
18
16
  /** @param socket {import('./Socket.js').Socket} */
19
- [join](socket) {
17
+ join(socket) {
20
18
  this.sockets.add(socket)
21
19
 
22
20
  socket.rooms.add(this)
@@ -24,7 +22,7 @@ export class Room extends Emitter {
24
22
  this.onJoin(socket)
25
23
  }
26
24
  /** @param socket {import('./Socket.js').Socket} */
27
- [leave](socket) {
25
+ leave(socket) {
28
26
  this.sockets.delete(socket)
29
27
 
30
28
  socket.rooms.delete(this)
package/server/Rooms.js CHANGED
@@ -1,4 +1,4 @@
1
- import { join, leave, parent } from './constants.js'
1
+ import { parent } from './constants.js'
2
2
  import { Emitter } from './Emitter.js'
3
3
  import { arrayFrom } from './utils.js'
4
4
 
@@ -30,13 +30,13 @@ export class Rooms extends Emitter {
30
30
 
31
31
  room.onCreate(socket)
32
32
  }
33
- room[join](socket)
33
+ room.join(socket)
34
34
  }
35
35
 
36
36
  leave(socket, id = undefined) {
37
37
  const room = this.rooms.get(id)
38
38
  if (room) {
39
- room[leave](socket)
39
+ room.leave(socket)
40
40
  if (!room.persistent && room.sockets.size === 0) {
41
41
  this.rooms.delete(id)
42
42
  room.onDelete(socket)
package/server/Socket.js CHANGED
@@ -97,7 +97,9 @@ export class Socket {
97
97
  this.server.sockets.delete(this)
98
98
 
99
99
  for (const room of this.rooms) {
100
- room[parent].leave(this, room.id)
100
+ room[parent]
101
+ ? room[parent].leave(this, room.id)
102
+ : room.leave(this)
101
103
  }
102
104
 
103
105
  this.server.events.disconnect &&
@@ -1,3 +1 @@
1
- export const join = Symbol('join')
2
- export const leave = Symbol('leave')
3
1
  export const parent = Symbol('parent')