angelia.io 4.0.86 → 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 +23 -7
- package/server/Rooms.js +7 -3
- package/server/Server.js +1 -1
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -211,6 +211,9 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
|
|
|
211
211
|
are created when the `id` of the room doesn't exists, and deleted
|
|
212
212
|
when there are no sockets in the room and the room doesn't have the
|
|
213
213
|
flag `persistent`
|
|
214
|
+
4. a socket will leave automatically all rooms on disconnection
|
|
215
|
+
5. `socket.rooms` is a set with all the rooms the socket joined
|
|
216
|
+
6. `id` is assigned to the room on creation
|
|
214
217
|
|
|
215
218
|
```js
|
|
216
219
|
import { Room, Rooms } from 'angelia.io/server'
|
|
@@ -241,13 +244,14 @@ const games = new Rooms(GameRoom)
|
|
|
241
244
|
|
|
242
245
|
class Connection {
|
|
243
246
|
connect(socket) {
|
|
244
|
-
|
|
245
|
-
socket.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
socket
|
|
250
|
-
game.
|
|
247
|
+
games.join(socket, 'room id here')
|
|
248
|
+
socket.game.id === 'room id here'
|
|
249
|
+
console.log(socket.rooms)
|
|
250
|
+
games.leave(socket, 'room id here')
|
|
251
|
+
|
|
252
|
+
games.join(socket, 'a different room')
|
|
253
|
+
socket.game.id === 'a different room'
|
|
254
|
+
games.leave(socket, 'a different room')
|
|
251
255
|
}
|
|
252
256
|
}
|
|
253
257
|
|
|
@@ -257,6 +261,8 @@ Server.listen()
|
|
|
257
261
|
|
|
258
262
|
### `Room` Class
|
|
259
263
|
|
|
264
|
+
Iterating `room` returns the sockets
|
|
265
|
+
|
|
260
266
|
| signature | kind | description |
|
|
261
267
|
| ------------------------------------- | ------- | ------------------------------------------------ |
|
|
262
268
|
| `onCreate(socket)` | method | dispatched when the room is created |
|
|
@@ -272,6 +278,16 @@ Server.listen()
|
|
|
272
278
|
| `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
|
|
273
279
|
| `id` | any | the room id |
|
|
274
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
|
+
|
|
275
291
|
## Authors
|
|
276
292
|
|
|
277
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
|
|
package/server/Server.js
CHANGED