angelia.io 4.0.85 → 4.0.86

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.
@@ -0,0 +1,13 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: [titoBouzout] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12
+ polar: # Replace with a single Polar username
13
+ custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "4.0.85",
3
+ "version": "4.0.86",
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
@@ -203,6 +203,75 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
203
203
  | `reconnect` | if we were connected at least once, then any re-connection will dispatch this event instead of `connect` |
204
204
  | `disconnect` | when we disconnect from the server |
205
205
 
206
+ ## Rooms
207
+
208
+ 1. Create a room class that extends `Room`
209
+ 2. Create a room list that will allow joining sockets
210
+ 3. Join a socket in a room, a socket may join multiple rooms. Rooms
211
+ are created when the `id` of the room doesn't exists, and deleted
212
+ when there are no sockets in the room and the room doesn't have the
213
+ flag `persistent`
214
+
215
+ ```js
216
+ import { Room, Rooms } from 'angelia.io/server'
217
+
218
+ class GameRoom extends Room {
219
+ persistent = true
220
+
221
+ onCreate(socket) {
222
+ console.log('creating room', this.id, socket)
223
+ }
224
+ onDelete(socket) {
225
+ console.log('deleting room', this.id, socket)
226
+ }
227
+
228
+ onJoin(socket) {
229
+ console.log('socket joined room', this.id, socket)
230
+ socket.game = this
231
+ this.emit('user joined', 'data here')
232
+ }
233
+ onLeave(socket) {
234
+ console.log('socket left room', this.id, socket)
235
+ socket.game = undefined
236
+ this.emit('user left', 'data here')
237
+ }
238
+ }
239
+
240
+ const games = new Rooms(GameRoom)
241
+
242
+ class Connection {
243
+ connect(socket) {
244
+ game.join(socket, 'room id here')
245
+ socket.room.id === 'room id here'
246
+ game.leave(socket, 'room id here')
247
+
248
+ game.join(socket, 'a different room')
249
+ socket.room.id === 'a different room'
250
+ game.leave(socket, 'a different room')
251
+ }
252
+ }
253
+
254
+ Server.on(Connection)
255
+ Server.listen()
256
+ ```
257
+
258
+ ### `Room` Class
259
+
260
+ | signature | kind | description |
261
+ | ------------------------------------- | ------- | ------------------------------------------------ |
262
+ | `onCreate(socket)` | method | dispatched when the room is created |
263
+ | `onDelete(socket)` | method | dispatched when the room is deleted |
264
+ | `onJoin(socket)` | method | dispatched when a socket joins the room |
265
+ | `onLeave(socket)` | method | dispatched when a socket leaves the room |
266
+ | `persistent` | boolean | to not delete rooms when there are no sockets in |
267
+ | `connections` | number | number of sockets in the room |
268
+ | `sockets` | Set | sockets in the room |
269
+ | `emit(key, [value])` | method | emits to all sockets in the room |
270
+ | `once(key, [value])` | method | emits once all sockets in the room |
271
+ | `broadcast(socket, key, [value])` | method | emits to other sockets in the room |
272
+ | `broadcastOnce(socket, key, [value])` | method | emits once to other sockets in the room |
273
+ | `id` | any | the room id |
274
+
206
275
  ## Authors
207
276
 
208
277
  - Tito Bouzout https://github.com/titoBouzout
@@ -0,0 +1,39 @@
1
+ export class Emitter {
2
+ constructor() {
3
+ /** @type Set<import('./Socket.js').Socket> */
4
+ this.sockets = new Set()
5
+ }
6
+
7
+ get connections() {
8
+ return this.sockets.size
9
+ }
10
+
11
+ emit(k, v) {
12
+ const d = [k, v]
13
+ for (const socket of this.sockets) {
14
+ socket.emit(d)
15
+ }
16
+ }
17
+ once(k, v) {
18
+ const d = [k, v]
19
+ for (const socket of this.sockets) {
20
+ socket.once(d)
21
+ }
22
+ }
23
+ broadcast(me, k, v) {
24
+ const d = [k, v]
25
+ for (const socket of this.sockets) {
26
+ if (me != socket) {
27
+ socket.emit(d)
28
+ }
29
+ }
30
+ }
31
+ broadcastOnce(me, k, v) {
32
+ const d = [k, v]
33
+ for (const socket of this.sockets) {
34
+ if (me != socket) {
35
+ socket.once(d)
36
+ }
37
+ }
38
+ }
39
+ }
package/server/Room.js ADDED
@@ -0,0 +1,32 @@
1
+ import { join, leave } from './constants.js'
2
+
3
+ import { Emitter } from './Emitter.js'
4
+
5
+ export class Room extends Emitter {
6
+ id
7
+ persistent
8
+
9
+ onCreate(socket) {}
10
+ onDelete(socket) {}
11
+ onJoin(socket) {}
12
+ onLeave(socket) {}
13
+
14
+ [join](socket) {
15
+ this.sockets.add(socket)
16
+
17
+ socket.rooms.add(this)
18
+
19
+ this.onJoin(socket)
20
+ }
21
+ [leave](socket) {
22
+ this.sockets.delete(socket)
23
+
24
+ socket.rooms.delete(this)
25
+
26
+ this.onLeave(socket)
27
+ }
28
+
29
+ [Symbol.iterator]() {
30
+ return this.sockets
31
+ }
32
+ }
@@ -0,0 +1,35 @@
1
+ import { join, leave } from './constants.js'
2
+
3
+ export class Rooms {
4
+ constructor(Room) {
5
+ this.Room = Room
6
+ this.rooms = new Map()
7
+ }
8
+ join(socket, id = undefined) {
9
+ let room = this.rooms.get(id)
10
+ if (!room) {
11
+ room = new this.Room()
12
+ room.id = room.id || id
13
+ this.rooms.set(id, room)
14
+
15
+ room.onCreate(socket)
16
+ }
17
+
18
+ room[join](socket)
19
+ }
20
+ leave(socket, id = undefined) {
21
+ const room = this.rooms.get(id)
22
+ if (room) {
23
+ room[leave](socket)
24
+
25
+ if (!room.persistent && room.size === 0) {
26
+ room.onDelete(socket)
27
+ this.rooms.delete(id)
28
+ }
29
+ }
30
+ }
31
+
32
+ [Symbol.iterator]() {
33
+ return this.rooms.values()
34
+ }
35
+ }
package/server/Server.js CHANGED
@@ -7,9 +7,8 @@ import {
7
7
  stringify,
8
8
  } from './utils.js'
9
9
 
10
- import { Manager } from './rooms/Manager.js'
11
- import { Room } from './rooms/Room.js'
12
10
  import { Socket } from './Socket.js'
11
+ import { Emitter } from './Emitter.js'
13
12
 
14
13
  import { nextTick } from 'node:process'
15
14
  import * as http from 'http'
@@ -17,11 +16,11 @@ import formidable from 'formidable'
17
16
 
18
17
  import { WebSocketServer } from 'ws'
19
18
 
20
- export { Room }
21
-
22
19
  /** Creates a new Socket Server */
23
- export default new (class Server {
20
+ export default new (class Server extends Emitter {
24
21
  constructor() {
22
+ super()
23
+
25
24
  this.hostname = ''
26
25
  this.port = 3001
27
26
 
@@ -44,8 +43,6 @@ export default new (class Server {
44
43
  this.serverErrors = 0
45
44
  this.socketErrors = 0
46
45
 
47
- this.sockets = new Set()
48
-
49
46
  this.queue = []
50
47
 
51
48
  this.cacheSymbol = Symbol('cache')
@@ -56,11 +53,6 @@ export default new (class Server {
56
53
 
57
54
  this.pingData = frame('')
58
55
  this.disconnectData = frame(stringify([['disconnect', true]]))
59
-
60
- this._track = Manager.track
61
- this.rooms = Manager.rooms
62
- this.Room = Room
63
- this.observe = Manager.observe
64
56
  }
65
57
  /**
66
58
  * Start server listening
@@ -72,7 +64,7 @@ export default new (class Server {
72
64
  * maxPostSize?: number
73
65
  * skipUTF8Validation?: boolean
74
66
  * timeout?: number
75
- * }} options
67
+ * }} [options]
76
68
  */
77
69
  listen(options) {
78
70
  this.hostname = options.hostname || this.hostname
@@ -157,15 +149,11 @@ export default new (class Server {
157
149
  console.log('Socket Server Started Listening On Port', this.port)
158
150
  }
159
151
 
160
- // count of connections
161
- get connections() {
162
- return this.sockets.size
163
- }
164
-
165
152
  /**
166
153
  * Listen for events
167
154
  *
168
- * @param {ObjectConstructor} listener
155
+ * @template T
156
+ * @param {T} listener
169
157
  */
170
158
  on(listener) {
171
159
  const instance = new listener()
@@ -189,35 +177,6 @@ export default new (class Server {
189
177
  }
190
178
  }
191
179
 
192
- emit(k, v) {
193
- const d = [k, v]
194
- for (const socket of this.sockets) {
195
- socket.emit(d)
196
- }
197
- }
198
- once(k, v) {
199
- const d = [k, v]
200
- for (const socket of this.sockets) {
201
- socket.once(d)
202
- }
203
- }
204
- broadcast(me, k, v) {
205
- const d = [k, v]
206
- for (const socket of this.sockets) {
207
- if (me != socket) {
208
- socket.emit(d)
209
- }
210
- }
211
- }
212
- broadcastOnce(me, k, v) {
213
- const d = [k, v]
214
- for (const socket of this.sockets) {
215
- if (me != socket) {
216
- socket.once(d)
217
- }
218
- }
219
- }
220
-
221
180
  // private
222
181
 
223
182
  onconnect = (socket, request) => {
@@ -229,7 +188,7 @@ export default new (class Server {
229
188
 
230
189
  this.sockets.add(socket)
231
190
 
232
- this.events.connect && this.events.connect(socket.proxy, request)
191
+ this.events.connect && this.events.connect(socket, request)
233
192
 
234
193
  if (socket.params.angelia) {
235
194
  socket.onmessage(socket.params.angelia)
@@ -288,8 +247,7 @@ export default new (class Server {
288
247
 
289
248
  if (delay > this.timeout) {
290
249
  socket.timedout = true
291
- this.events.timeout &&
292
- this.events.timeout(socket.proxy, delay)
250
+ this.events.timeout && this.events.timeout(socket, delay)
293
251
  socket.io.terminate()
294
252
  } else if (delay > this.timeoutCheck) {
295
253
  /**
@@ -318,7 +276,7 @@ export default new (class Server {
318
276
  socket.seen = this.now
319
277
  socket.ping = this.now - socket.contacted
320
278
 
321
- this.events.ping && this.events.ping(socket.proxy)
279
+ this.events.ping && this.events.ping(socket)
322
280
  }
323
281
 
324
282
  toJSON() {
@@ -330,12 +288,4 @@ export default new (class Server {
330
288
  sockets: 'omitted',
331
289
  }
332
290
  }
333
-
334
- track(path) {
335
- this.tracking = true
336
- for (const socket of this.sockets) {
337
- socket.proxy = this.observe(socket)
338
- }
339
- return this._track(path)
340
- }
341
291
  })()
package/server/Socket.js CHANGED
@@ -1,4 +1,4 @@
1
- import { leave } from './rooms/constants.js'
1
+ import { leave } from './constants.js'
2
2
 
3
3
  import { fromEntries, inspect, isArray, parse } from './utils.js'
4
4
 
@@ -35,8 +35,6 @@ export class Socket {
35
35
  this.timedout = false
36
36
 
37
37
  this.rooms = new Set()
38
-
39
- this.proxy = server.tracking ? server.observe(this) : this
40
38
  }
41
39
  emit(k, v, cb) {
42
40
  if (!this.messages.length) {
@@ -93,11 +91,11 @@ export class Socket {
93
91
  this.server.sockets.delete(this)
94
92
 
95
93
  for (const room of this.rooms) {
96
- room[leave](this.proxy)
94
+ room[leave](this)
97
95
  }
98
96
 
99
97
  this.server.events.disconnect &&
100
- this.server.events.disconnect(this.proxy, code, message)
98
+ this.server.events.disconnect(this, code, message)
101
99
  }
102
100
  onerror = err => {
103
101
  this.server.socketErrors++
@@ -149,26 +147,26 @@ export class Socket {
149
147
  this.messagesReceived += messages.length
150
148
 
151
149
  this.server.events.incoming &&
152
- this.server.events.incoming(this.proxy, messages)
150
+ this.server.events.incoming(this, messages)
153
151
  for (const m of messages) {
154
152
  if (m[0] === '') {
155
153
  this.oncallback(m[1], m)
156
154
  } else if (this.server.events[m[0]]) {
157
155
  this.server.events[m[0]](
158
- this.proxy,
156
+ this,
159
157
  m[1],
160
158
  m[2] && this.doreply.bind(null, m[2]),
161
159
  )
162
160
  } else {
163
161
  this.server.messagesGarbage++
164
162
  this.server.events.garbage &&
165
- this.server.events.garbage(this.proxy, m)
163
+ this.server.events.garbage(this, m)
166
164
  }
167
165
  }
168
166
  } else {
169
167
  this.server.messagesGarbage++
170
168
  this.server.events.garbage &&
171
- this.server.events.garbage(this.proxy, messages || e)
169
+ this.server.events.garbage(this, messages || e)
172
170
  }
173
171
  }
174
172
  }
@@ -179,7 +177,7 @@ export class Socket {
179
177
  this.messages = []
180
178
 
181
179
  this.server.events.outgoing &&
182
- this.server.events.outgoing(this.proxy, messages)
180
+ this.server.events.outgoing(this, messages)
183
181
 
184
182
  this.server.messagesSent += messages.length
185
183
  this.messagesSent += messages.length
@@ -210,8 +208,6 @@ export class Socket {
210
208
  callbacks: 'omitted',
211
209
 
212
210
  readyState: this.io.readyState,
213
-
214
- proxy: 'omitted',
215
211
  }
216
212
  }
217
213
  }
@@ -0,0 +1,2 @@
1
+ export const join = Symbol('join')
2
+ export const leave = Symbol('leave')
package/server/index.js CHANGED
@@ -1 +1,3 @@
1
- export { Room, default } from './Server.js'
1
+ export { default } from './Server.js'
2
+ export { Room } from './Room.js'
3
+ export { Rooms } from './Rooms.js'
@@ -0,0 +1,9 @@
1
+ export class Emitter {
2
+ /** @type Set<import('./Socket.js').Socket> */
3
+ sockets: Set<import("./Socket.js").Socket>;
4
+ get connections(): number;
5
+ emit(k: any, v: any): void;
6
+ once(k: any, v: any): void;
7
+ broadcast(me: any, k: any, v: any): void;
8
+ broadcastOnce(me: any, k: any, v: any): void;
9
+ }
@@ -0,0 +1,14 @@
1
+ export class Room extends Emitter {
2
+ id: any;
3
+ persistent: any;
4
+ onCreate(socket: any): void;
5
+ onDelete(socket: any): void;
6
+ onJoin(socket: any): void;
7
+ onLeave(socket: any): void;
8
+ [join](socket: any): void;
9
+ [leave](socket: any): void;
10
+ [Symbol.iterator](): Set<import("./Socket.js").Socket>;
11
+ }
12
+ import { Emitter } from './Emitter.js';
13
+ import { join } from './constants.js';
14
+ import { leave } from './constants.js';
@@ -0,0 +1,8 @@
1
+ export class Rooms {
2
+ constructor(Room: any);
3
+ Room: any;
4
+ rooms: Map<any, any>;
5
+ join(socket: any, id?: undefined): void;
6
+ leave(socket: any, id?: undefined): void;
7
+ [Symbol.iterator](): MapIterator<any>;
8
+ }
@@ -1,4 +1,3 @@
1
- export { Room };
2
1
  declare const _default: {
3
2
  hostname: string;
4
3
  port: number;
@@ -17,7 +16,6 @@ declare const _default: {
17
16
  messagesSentCacheHit: number;
18
17
  serverErrors: number;
19
18
  socketErrors: number;
20
- sockets: Set<any>;
21
19
  queue: any[];
22
20
  cacheSymbol: symbol;
23
21
  cached: number;
@@ -25,10 +23,6 @@ declare const _default: {
25
23
  events: any;
26
24
  pingData: any;
27
25
  disconnectData: any;
28
- _track: (path: any) => any;
29
- rooms: any;
30
- Room: typeof Room;
31
- observe: (socket: any) => any;
32
26
  /**
33
27
  * Start server listening
34
28
  *
@@ -39,27 +33,23 @@ declare const _default: {
39
33
  * maxPostSize?: number
40
34
  * skipUTF8Validation?: boolean
41
35
  * timeout?: number
42
- * }} options
36
+ * }} [options]
43
37
  */
44
- listen(options: {
38
+ listen(options?: {
45
39
  hostname?: string;
46
40
  port?: number;
47
41
  maxMessageSize?: number;
48
42
  maxPostSize?: number;
49
43
  skipUTF8Validation?: boolean;
50
44
  timeout?: number;
51
- }): void;
52
- readonly connections: number;
45
+ } | undefined): void;
53
46
  /**
54
47
  * Listen for events
55
48
  *
56
- * @param {ObjectConstructor} listener
49
+ * @template T
50
+ * @param {T} listener
57
51
  */
58
- on(listener: ObjectConstructor): void;
59
- emit(k: any, v: any): void;
60
- once(k: any, v: any): void;
61
- broadcast(me: any, k: any, v: any): void;
62
- broadcastOnce(me: any, k: any, v: any): void;
52
+ on<T>(listener: T): void;
63
53
  onconnect: (socket: any, request: any) => void;
64
54
  onerror: (err: any) => void;
65
55
  nextQueue(socket: any): void;
@@ -70,12 +60,16 @@ declare const _default: {
70
60
  pingSocket(socket: any): void;
71
61
  pong(socket: any): void;
72
62
  toJSON(): string;
73
- track(path: any): any;
74
- tracking: boolean | undefined;
75
63
  [inspect](): any & {
76
64
  sockets: string;
77
65
  };
66
+ sockets: Set<import("./Socket.js").Socket>;
67
+ readonly connections: number;
68
+ emit(k: any, v: any): void;
69
+ once(k: any, v: any): void;
70
+ broadcast(me: any, k: any, v: any): void;
71
+ broadcastOnce(me: any, k: any, v: any): void;
78
72
  };
79
73
  export default _default;
80
- import { Room } from './rooms/Room.js';
81
74
  import { inspect } from './utils.js';
75
+ import { Socket } from './Socket.js';
@@ -21,7 +21,6 @@ export class Socket {
21
21
  ping: number;
22
22
  timedout: boolean;
23
23
  rooms: Set<any>;
24
- proxy: any;
25
24
  emit(k: any, v: any, cb: any): void;
26
25
  once(k: any, v: any): void;
27
26
  disconnect(noReconnect: any): void;
@@ -43,7 +42,6 @@ export class Socket {
43
42
  messages: string;
44
43
  callbacks: string;
45
44
  readyState: any;
46
- proxy: string;
47
45
  };
48
46
  }
49
47
  import { inspect } from './utils.js';
@@ -0,0 +1,2 @@
1
+ export const join: unique symbol;
2
+ export const leave: unique symbol;
@@ -1 +1,3 @@
1
- export { Room, default } from "./Server.js";
1
+ export { default } from "./Server.js";
2
+ export { Room } from "./Room.js";
3
+ export { Rooms } from "./Rooms.js";
@@ -1,108 +0,0 @@
1
- import { join, leave } from './constants.js'
2
-
3
- import { Rooms } from './Rooms.js'
4
-
5
- class ManagerSingleton {
6
- constructor() {
7
- Object.assign(this, {
8
- paths: {}, // path tree as string
9
- pathsObject: {}, // path tree as object
10
-
11
- rooms: new Map(),
12
-
13
- proxied: Symbol('proxied'),
14
-
15
- track: this.track.bind(this),
16
- observe: this.observe.bind(this),
17
- })
18
- }
19
- track(path) {
20
- const root = '.' + path
21
-
22
- if (!this.rooms.has(root)) {
23
- // index for fast access
24
- let paths = ''
25
- let pathsObject = this.pathsObject
26
- for (const child of path.split('.')) {
27
- paths += '.' + child
28
- pathsObject = pathsObject[child] = pathsObject[child] || {}
29
- this.paths[paths] = pathsObject
30
- }
31
-
32
- // room list
33
- this.rooms.set(root, new Rooms(root))
34
-
35
- // console.log('New path to track "' + path + '"', this.paths)
36
- }
37
- return this.rooms.get(root)
38
- }
39
-
40
- observe(socket) {
41
- return this.proxy(socket, '', socket)
42
- }
43
-
44
- proxy(target, path, socket) {
45
- if (!target[this.proxied]) {
46
- target[this.proxied] = true
47
- target = new Proxy(target, {
48
- set: (target, id, value, receiver) => {
49
- // if not Symbol and path in track
50
- if (typeof id === 'string' && this.paths[path + '.' + id]) {
51
- const oldValue = target[id]
52
- target[id] = value
53
-
54
- this.check(
55
- socket,
56
- path + '.' + id,
57
- target,
58
- oldValue,
59
- value,
60
- receiver,
61
- )
62
- } else {
63
- target[id] = value
64
- }
65
- return true
66
- },
67
- })
68
- }
69
- return target
70
- }
71
- check(socket, path, target, oldValue, newValue, receiver) {
72
- if (this.rooms.has(path)) {
73
- if (oldValue !== newValue) {
74
- /*console.log(
75
- 'YES "' + path + '"',
76
- 'changed from',
77
- oldValue,
78
- 'to',
79
- newValue,
80
- )*/
81
-
82
- // LEAVE
83
- // we wont leave undefined or null
84
- if (typeof oldValue === 'object' && oldValue !== null) {
85
- oldValue[leave](socket.proxy)
86
- // console.log('leaving room', oldValue)
87
- }
88
-
89
- // JOIN
90
- // we wont join undefined and null
91
- if (typeof newValue === 'object' && newValue !== null) {
92
- newValue[join](this.rooms.get(path), socket.proxy)
93
- // console.log('joining room', newValue)
94
- }
95
- } else {
96
- // console.log('value didnt change', path, oldValue, newValue)
97
- }
98
- } else {
99
- // console.log('not tracking path', path)
100
- }
101
- }
102
-
103
- [Symbol.iterator]() {
104
- return this.rooms[Symbol.iterator]()
105
- }
106
- }
107
-
108
- export const Manager = new ManagerSingleton()
@@ -1,97 +0,0 @@
1
- import { join, leave, parent, add, remove } from './constants.js'
2
-
3
- export class Room {
4
- constructor() {
5
- this.sockets = new Set()
6
- }
7
- has(socket) {
8
- return this.sockets.has(socket)
9
- }
10
- get size() {
11
- return this.sockets.size
12
- }
13
- // private
14
- [join](roomList, socket) {
15
- // add to list of room list
16
- if (!this[parent]) {
17
- this[parent] = roomList
18
- this[parent][add](this)
19
- }
20
-
21
- // add user
22
- this.sockets.add(socket)
23
-
24
- // to be able to list rooms for a socket
25
- socket.rooms.add(this)
26
-
27
- // if the room has the property "persistent"
28
- // then maybe the room is old and someone just joined
29
- // for this reason we dont dispatch onCreate for persistent rooms
30
- // as that would be misleading
31
- if (!this.persistent && this.sockets.size === 1) {
32
- // dispatch create
33
- this.onCreate && this.onCreate(socket)
34
- }
35
-
36
- // dispatch join
37
- this.onJoin && this.onJoin(socket)
38
- }
39
- [leave](socket) {
40
- // remove user
41
- this.sockets.delete(socket)
42
-
43
- // to be able to list rooms for socket
44
- socket.rooms.delete(this)
45
-
46
- // check room deletion
47
- if (!this.persistent && this.sockets.size === 0) {
48
- // remove room from list
49
- this[parent][remove](this)
50
-
51
- // dispatch leave
52
- // this.onLeave && this.onLeave(socket)
53
-
54
- // dispatch that the room has been deleted
55
- this.onDelete && this.onDelete(socket)
56
- } else {
57
- // dispatch leave
58
- this.onLeave && this.onLeave(socket)
59
- }
60
- }
61
-
62
- emit(k, v) {
63
- const d = [k, v]
64
-
65
- for (const socket of this.sockets) {
66
- socket.emit(d)
67
- }
68
- }
69
- once(k, v) {
70
- const d = [k, v]
71
-
72
- for (const socket of this.sockets) {
73
- socket.once(d)
74
- }
75
- }
76
- broadcast(me, k, v) {
77
- const d = [k, v]
78
-
79
- for (const socket of this.sockets) {
80
- if (me != socket) socket.emit(d)
81
- }
82
- }
83
- broadcastOnce(me, k, v) {
84
- const d = [k, v]
85
-
86
- for (const socket of this.sockets) {
87
- if (me != socket) socket.once(d)
88
- }
89
- }
90
-
91
- toJSON() {
92
- return this.sockets
93
- }
94
- [Symbol.iterator]() {
95
- return this.sockets[Symbol.iterator]()
96
- }
97
- }
@@ -1,36 +0,0 @@
1
- import { add, remove } from './constants.js'
2
-
3
- export class Rooms {
4
- constructor(path) {
5
- Object.assign(this, {
6
- path: path,
7
- rooms: new Set(),
8
- roomsById: new Map(),
9
- })
10
- }
11
-
12
- get(id) {
13
- return this.roomsById.get(id)
14
- }
15
- get size() {
16
- return this.rooms.size
17
- }
18
- // private
19
- [add](room) {
20
- if (!this.rooms.has(room)) {
21
- this.rooms.add(room)
22
- this.roomsById.set(room.id, room)
23
- }
24
- }
25
- [remove](room) {
26
- this.rooms.delete(room)
27
- this.roomsById.delete(room.id)
28
- }
29
-
30
- toJSON() {
31
- return this.rooms
32
- }
33
- [Symbol.iterator]() {
34
- return this.rooms[Symbol.iterator]()
35
- }
36
- }
@@ -1,6 +0,0 @@
1
- export const join = Symbol.for('Room.join')
2
- export const leave = Symbol.for('Room.leave')
3
- export const parent = Symbol.for('Room.parent')
4
-
5
- export const add = Symbol.for('Rooms.add')
6
- export const remove = Symbol.for('Rooms.remove')
@@ -1,9 +0,0 @@
1
- export const Manager: ManagerSingleton;
2
- declare class ManagerSingleton {
3
- track(path: any): any;
4
- observe(socket: any): any;
5
- proxy(target: any, path: any, socket: any): any;
6
- check(socket: any, path: any, target: any, oldValue: any, newValue: any, receiver: any): void;
7
- [Symbol.iterator](): any;
8
- }
9
- export {};
@@ -1,17 +0,0 @@
1
- export class Room {
2
- sockets: Set<any>;
3
- has(socket: any): boolean;
4
- get size(): number;
5
- emit(k: any, v: any): void;
6
- once(k: any, v: any): void;
7
- broadcast(me: any, k: any, v: any): void;
8
- broadcastOnce(me: any, k: any, v: any): void;
9
- toJSON(): Set<any>;
10
- [join](roomList: any, socket: any): void;
11
- [leave](socket: any): void;
12
- [Symbol.iterator](): SetIterator<any>;
13
- [parent]: any;
14
- }
15
- import { join } from './constants.js';
16
- import { leave } from './constants.js';
17
- import { parent } from './constants.js';
@@ -1,11 +0,0 @@
1
- export class Rooms {
2
- constructor(path: any);
3
- get(id: any): any;
4
- get size(): any;
5
- toJSON(): any;
6
- [add](room: any): void;
7
- [remove](room: any): void;
8
- [Symbol.iterator](): any;
9
- }
10
- import { add } from './constants.js';
11
- import { remove } from './constants.js';
@@ -1,5 +0,0 @@
1
- export const join: unique symbol;
2
- export const leave: unique symbol;
3
- export const parent: unique symbol;
4
- export const add: unique symbol;
5
- export const remove: unique symbol;