angelia.io 4.0.89 → 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/client/worker.js +12 -2
- package/package.json +1 -1
- package/readme.md +64 -22
- package/server/Room.js +2 -4
- package/server/Rooms.js +3 -3
- package/server/Socket.js +3 -1
- package/server/constants.js +0 -2
package/client/worker.js
CHANGED
|
@@ -70,6 +70,12 @@ export function ClientWebWorker() {
|
|
|
70
70
|
* event.reason)
|
|
71
71
|
*/
|
|
72
72
|
break
|
|
73
|
+
case 1001:
|
|
74
|
+
/**
|
|
75
|
+
* Freaking firefox code when reloading the page, must be
|
|
76
|
+
* blacklisted else it will connect twice in reloads.
|
|
77
|
+
*/
|
|
78
|
+
break
|
|
73
79
|
case 1005:
|
|
74
80
|
/**
|
|
75
81
|
* Closed by client - console.log( 'ws - we called
|
|
@@ -97,8 +103,12 @@ export function ClientWebWorker() {
|
|
|
97
103
|
postMessage(['dispatch', 'disconnect'])
|
|
98
104
|
}
|
|
99
105
|
|
|
100
|
-
|
|
101
|
-
|
|
106
|
+
console.log(code)
|
|
107
|
+
|
|
108
|
+
// 1001: freaking firefox
|
|
109
|
+
// 1005: do not reconnect when we call `socket.disconnect()`
|
|
110
|
+
// 1006: DO NOT USE! THAT HAPPENS WHEN SERVER CONNECTION IS KILLED
|
|
111
|
+
if (reconnect && code !== 1005 && code !== 1001) {
|
|
102
112
|
postMessage(['connect'])
|
|
103
113
|
}
|
|
104
114
|
}
|
package/package.json
CHANGED
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.
|
|
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 =
|
|
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
|
-
|
|
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`
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
|
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
|
|
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]
|
|
100
|
+
room[parent]
|
|
101
|
+
? room[parent].leave(this, room.id)
|
|
102
|
+
: room.leave(this)
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
this.server.events.disconnect &&
|
package/server/constants.js
CHANGED