angelia.io 2.3.68 → 2.4.69

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": "2.3.68",
3
+ "version": "2.4.69",
4
4
  "description": "WebSockets Server and Client API for node.js and the browser, with rooms support.",
5
5
  "homepage": "https://github.com/titoBouzout/angelia.io",
6
6
  "repository": {
package/server/Server.js CHANGED
@@ -141,7 +141,11 @@ class ServerSingleton {
141
141
  process.on(m, () => {
142
142
  if (!io.closing) {
143
143
  io.closing = true
144
- console.log('Server Shutting Down\n', this)
144
+ if (options.debug) {
145
+ console.log('Server Shutting Down\n', this)
146
+ } else {
147
+ console.log('Server Shutting Down\n')
148
+ }
145
149
  io.close()
146
150
  if (options.debug) {
147
151
  process.exit()
@@ -150,7 +154,7 @@ class ServerSingleton {
150
154
  })
151
155
  }
152
156
 
153
- console.log('Server Started Listening On Port ' + this.port)
157
+ console.log('Socket Server Started Listening On Port ', this.port)
154
158
  this.events.listen && this.events.listen()
155
159
 
156
160
  return this
@@ -1,90 +1,96 @@
1
1
  'use strict'
2
2
 
3
- const { join, leave, parent } = require('./constants.js')
3
+ const { join, leave, parent, add, remove } = require('./constants.js')
4
4
 
5
5
  class Room {
6
6
  constructor() {
7
- this.users = []
7
+ this.sockets = new Set()
8
8
  }
9
9
  has(socket) {
10
- return this.users.indexOf(socket) !== -1
10
+ return this.sockets.has(socket)
11
11
  }
12
12
  [join](roomList, socket) {
13
13
  // add to list of room list
14
14
  if (!this[parent]) {
15
15
  this[parent] = roomList
16
- this[parent].add(this)
16
+ this[parent][add](this)
17
17
  }
18
18
 
19
19
  // add user
20
- this.users.push(socket)
20
+ this.sockets.add(socket)
21
21
 
22
- // to be able to list rooms for socket
22
+ // to be able to list rooms for a socket
23
23
  socket.rooms.add(this)
24
24
 
25
- if (this.users.length === 1) {
25
+ // if the room has the property "persistent"
26
+ // then maybe the room is old and someone just joined
27
+ // for this reason we dont dispatch onCreate for persistent rooms
28
+ // as that would be misleading
29
+ if (!this.persistent && this.sockets.size === 1) {
26
30
  // dispatch create
27
- this.create && this.create(socket)
31
+ this.onCreate && this.onCreate(socket)
28
32
  }
29
33
 
30
34
  // dispatch join
31
- this.join && this.join(socket)
35
+ this.onJoin && this.onJoin(socket)
32
36
  }
33
37
  [leave](socket) {
34
38
  // remove user
35
- let index = this.users.indexOf(socket)
36
- if (index !== -1) this.users.splice(index, 1)
39
+ this.sockets.delete(socket)
37
40
 
38
41
  // to be able to list rooms for socket
39
42
  socket.rooms.delete(this)
40
43
 
41
- // check removal
42
- if (this.users.length === 0 && !this.persistent) {
44
+ // check room deletion
45
+ if (!this.persistent && this.sockets.size === 0) {
43
46
  // remove room from list
44
- this[parent].delete(this)
47
+ this[parent][remove](this)
45
48
 
46
49
  // dispatch leave
47
- this.leave && this.leave(socket)
50
+ this.onLeave && this.onLeave(socket)
48
51
 
49
52
  // dispatch that the room has been deleted
50
- this.delete && this.delete(socket)
53
+ this.onDelete && this.onDelete(socket)
51
54
  } else {
52
55
  // dispatch leave
53
- this.leave && this.leave(socket)
56
+ this.onLeave && this.onLeave(socket)
54
57
  }
55
58
  }
56
59
 
57
60
  emit(k, v) {
58
61
  let d = [k, v]
59
62
 
60
- for (let socket of this.users) {
63
+ for (let socket of this.sockets) {
61
64
  socket.emit(d)
62
65
  }
63
66
  }
64
67
  once(k, v) {
65
68
  let d = [k, v]
66
69
 
67
- for (let socket of this.users) {
70
+ for (let socket of this.sockets) {
68
71
  socket.once(d)
69
72
  }
70
73
  }
71
74
  broadcast(me, k, v) {
72
75
  let d = [k, v]
73
76
 
74
- for (let socket of this.users) {
77
+ for (let socket of this.sockets) {
75
78
  if (me != socket) socket.emit(d)
76
79
  }
77
80
  }
78
81
  broadcastOnce(me, k, v) {
79
82
  let d = [k, v]
80
83
 
81
- for (let socket of this.users) {
84
+ for (let socket of this.sockets) {
82
85
  if (me != socket) socket.once(d)
83
86
  }
84
87
  }
85
88
 
89
+ toJSON() {
90
+ return this.sockets
91
+ }
86
92
  [Symbol.iterator]() {
87
- return this.users
93
+ return this.sockets
88
94
  }
89
95
  }
90
96
 
@@ -1,83 +1,35 @@
1
1
  'use strict'
2
2
 
3
- const { parent } = require('./constants.js')
3
+ const { add, remove } = require('./constants.js')
4
4
 
5
5
  class Rooms {
6
6
  constructor(path) {
7
7
  Object.assign(this, {
8
8
  path: path,
9
- rooms: [],
9
+ rooms: new Set(),
10
10
  roomsById: new Map(),
11
11
  })
12
12
  }
13
13
 
14
- has(room) {
15
- return this.rooms.indexOf(room) !== -1
16
- }
17
-
18
14
  get(id) {
19
15
  return this.roomsById.get(id)
20
16
  }
21
17
 
22
- add(room) {
23
- if (!this.has(room)) {
24
- this.rooms.push(room)
18
+ // private
19
+ [add](room) {
20
+ if (!this.rooms.has(room)) {
21
+ this.rooms.add(room)
25
22
  this.roomsById.set(room.id, room)
26
23
  }
27
24
  }
28
- delete(room) {
29
- if (!room.persistent) {
30
- let index = this.rooms.indexOf(room)
31
- if (index !== -1) this.rooms.splice(index, 1)
32
- this.roomsById.delete(room.id)
33
- }
34
- }
35
-
36
- emit(k, v) {
37
- let d = [k, v]
38
-
39
- for (let room of this.rooms) {
40
- for (let socket of room.users) {
41
- socket.emit(d)
42
- }
43
- }
44
- }
45
- once(k, v) {
46
- let d = [k, v]
47
-
48
- for (let room of this.rooms) {
49
- for (let socket of room.users) {
50
- socket.once(d)
51
- }
52
- }
53
- }
54
- broadcast(me, k, v) {
55
- let d = [k, v]
56
-
57
- for (let room of this.rooms) {
58
- for (let socket of room.users) {
59
- if (me != socket) {
60
- socket.emit(d)
61
- }
62
- }
63
- }
64
- }
65
- broadcastOnce(me, k, v) {
66
- let d = [k, v]
67
-
68
- for (let room of this.rooms) {
69
- for (let socket of room.users) {
70
- if (me != socket) {
71
- socket.once(d)
72
- }
73
- }
74
- }
25
+ [remove](room) {
26
+ this.rooms.delete(room)
27
+ this.roomsById.delete(room.id)
75
28
  }
76
29
 
77
30
  toJSON() {
78
31
  return this.rooms
79
32
  }
80
-
81
33
  [Symbol.iterator]() {
82
34
  return this.rooms
83
35
  }
@@ -2,8 +2,16 @@ const join = Symbol.for('Room.join')
2
2
  const leave = Symbol.for('Room.leave')
3
3
  const parent = Symbol.for('Room.parent')
4
4
 
5
+ const add = Symbol.for('Rooms.add')
6
+ const remove = Symbol.for('Rooms.remove')
7
+
5
8
  module.exports = {
9
+ // Room
6
10
  join,
7
11
  leave,
8
12
  parent,
13
+
14
+ // Rooms
15
+ add,
16
+ remove,
9
17
  }