angelia.io 4.0.87 → 4.0.88
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 +1 -1
- package/readme.md +19 -8
- package/server/Rooms.js +7 -3
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -213,8 +213,7 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
|
|
|
213
213
|
flag `persistent`
|
|
214
214
|
4. a socket will leave automatically all rooms on disconnection
|
|
215
215
|
5. `socket.rooms` is a set with all the rooms the socket joined
|
|
216
|
-
6.
|
|
217
|
-
room
|
|
216
|
+
6. `id` is assigned to the room on creation
|
|
218
217
|
|
|
219
218
|
```js
|
|
220
219
|
import { Room, Rooms } from 'angelia.io/server'
|
|
@@ -245,14 +244,14 @@ const games = new Rooms(GameRoom)
|
|
|
245
244
|
|
|
246
245
|
class Connection {
|
|
247
246
|
connect(socket) {
|
|
248
|
-
|
|
249
|
-
socket.
|
|
247
|
+
games.join(socket, 'room id here')
|
|
248
|
+
socket.game.id === 'room id here'
|
|
250
249
|
console.log(socket.rooms)
|
|
251
|
-
|
|
250
|
+
games.leave(socket, 'room id here')
|
|
252
251
|
|
|
253
|
-
|
|
254
|
-
socket.
|
|
255
|
-
|
|
252
|
+
games.join(socket, 'a different room')
|
|
253
|
+
socket.game.id === 'a different room'
|
|
254
|
+
games.leave(socket, 'a different room')
|
|
256
255
|
}
|
|
257
256
|
}
|
|
258
257
|
|
|
@@ -262,6 +261,8 @@ Server.listen()
|
|
|
262
261
|
|
|
263
262
|
### `Room` Class
|
|
264
263
|
|
|
264
|
+
Iterating `room` returns the sockets
|
|
265
|
+
|
|
265
266
|
| signature | kind | description |
|
|
266
267
|
| ------------------------------------- | ------- | ------------------------------------------------ |
|
|
267
268
|
| `onCreate(socket)` | method | dispatched when the room is created |
|
|
@@ -277,6 +278,16 @@ Server.listen()
|
|
|
277
278
|
| `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
|
|
278
279
|
| `id` | any | the room id |
|
|
279
280
|
|
|
281
|
+
### `Rooms` Class
|
|
282
|
+
|
|
283
|
+
Iterating `rooms` returns the rooms
|
|
284
|
+
|
|
285
|
+
| signature | kind | description |
|
|
286
|
+
| --------------------- | ------ | ------------------------------ |
|
|
287
|
+
| `get(id)` | method | returns 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
|
+
|
|
280
291
|
## Authors
|
|
281
292
|
|
|
282
293
|
- Tito Bouzout https://github.com/titoBouzout
|
package/server/Rooms.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { join, leave } from './constants.js'
|
|
2
2
|
|
|
3
3
|
export class Rooms {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/** @param {Object} RoomClass */
|
|
5
|
+
constructor(RoomClass) {
|
|
6
|
+
this.RoomClass = RoomClass
|
|
6
7
|
this.rooms = new Map()
|
|
7
8
|
}
|
|
9
|
+
get(id) {
|
|
10
|
+
return this.rooms.get(id)
|
|
11
|
+
}
|
|
8
12
|
join(socket, id = undefined) {
|
|
9
13
|
let room = this.rooms.get(id)
|
|
10
14
|
if (!room) {
|
|
11
|
-
room = new this.
|
|
15
|
+
room = new this.RoomClass()
|
|
12
16
|
room.id = room.id || id
|
|
13
17
|
this.rooms.set(id, room)
|
|
14
18
|
|