angelia.io 2.4.71 → 2.4.74

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.4.71",
3
+ "version": "2.4.74",
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/readme.md CHANGED
@@ -81,14 +81,16 @@ Server.listen({
81
81
 
82
82
  ### `Server` Options
83
83
 
84
- | name | kind | default | description |
85
- | -------------------- | ------- | ------- | --------------------------------------------------- |
86
- | `hostname` | String | '' | the hostname if any |
87
- | `port` | Number | 3001 | the port to use for this server |
88
- | `maxMessageSize` | Number | 5 | max size in mb of a message received |
89
- | `skipUTF8Validation` | Boolean | false | allows to skip utf8 validation |
90
- | `cert` | String | '' | path to the cert file for using https fullchain.pem |
91
- | `key` | String | '' | path to the key file for using https privkey.pem |
84
+ | name | kind | default | description |
85
+ | -------------------- | ----------- | ------- | ------------------------------------------------------------------------------------------------------------ |
86
+ | `hostname` | String | '' | the hostname if any |
87
+ | `port` | Number | 3001 | the port to use for this server |
88
+ | `maxMessageSize` | Number | 5 | max size in mb of a message received |
89
+ | `skipUTF8Validation` | Boolean | false | allows to skip utf8 validation |
90
+ | `cert` | String | '' | path to the cert file for using https fullchain.pem |
91
+ | `key` | String | '' | path to the key file for using https privkey.pem |
92
+ | `http` | node server | null | in case you want to use your own server. Else one will be created, as in `require('http(s)').createServer()` |
93
+ | `timeout` | Number | 60000 | time in milliseconds after a socket is considered gone, minimun value is 10000 |
92
94
 
93
95
  ### Server Object
94
96
 
@@ -115,29 +117,32 @@ Server.listen({
115
117
  <details>
116
118
  <summary><b>Expand</b></summary>
117
119
 
118
- | signature | kind | description |
119
- | ------------------------------------- | -------- | ---------------------------------------------------------------------------------------------- |
120
- | `since` | Number | timestamp of initialization |
121
- | `port` | Number | port used by this server |
122
- | `maxMessageSize` | Number | maximum message size in mb |
123
- | `timeout` | Number | after how long the socket is considered gone, in ms |
124
- | `connections` | Number | count of sockets connected |
125
- | `served` | Number | count of sockets ever connected |
126
- | `bytesSent` | Number | sum of bytes sent by the server |
127
- | `bytesReceived` | Number | sum of bytes the server has ever received |
128
- | `messagesSent` | Number | count of messages ever sent |
129
- | `messagesReceived` | Number | count of messages ever received |
130
- | `events` | Object | ref to events, ex: server.events.typing() to dispatch typing to anyone listening to it |
131
- | `listeners` | Array | for debbuging: array of listeners as strings |
132
- | `on(Class)` | Function | attaches all methods of a `Class` as listeners |
133
- | `on(Function)` | Function | attaches a named `Function` as a listener |
134
- | `on(Object)` | Function | attaches all properties of an object that are of the type `Function` as listeners |
135
- | `on(key, Function)` | Function | attaches a `Function` as a listener for `key` |
136
- | `emit(key, [value])` | Function | emits to all connected sockets |
137
- | `once(key, [value])` | Function | emits to the sockets and replace if exists a pending message with the same `key` |
138
- | `broadcast(sender, key, [value])` | Function | emits to all connected sockets except sender |
139
- | `broadcastOnce(sender, key, [value])` | Function | emits to the sockets except sender and replace if exists a pending message with the same `key` |
140
- | `sockets` | Set | a Set() with all the current connected sockets |
120
+ | signature | kind | description |
121
+ | ------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------- |
122
+ | `since` | Number | timestamp of initialization |
123
+ | `now` | Number | timestamp that updates every half a second |
124
+ | `port` | Number | port used by this server |
125
+ | `hostname` | String | the hostname if any |
126
+ | `maxMessageSize` | Number | maximum message size in mb |
127
+ | `timeout` | Number | after how long the socket is considered gone, in ms |
128
+ | `connections` | Number | count of sockets connected |
129
+ | `served` | Number | count of sockets ever connected |
130
+ | `bytesSent` | Number | sum of bytes sent by the server |
131
+ | `bytesReceived` | Number | sum of bytes the server has ever received |
132
+ | `messagesSent` | Number | count of messages ever sent |
133
+ | `messagesReceived` | Number | count of messages ever received |
134
+ | `events` | Object | ref to events, ex: server.events.typing() to dispatch typing to anyone listening to it |
135
+ | `listeners` | Array | for debbuging: array of listeners as strings |
136
+ | `on(Class)` | Function | attaches all methods of a `Class` as listeners |
137
+ | `on(Function)` | Function | attaches a named `Function` as a listener |
138
+ | `on(Object)` | Function | attaches all properties of an object that are of the type `Function` as listeners |
139
+ | `on(key, Function)` | Function | attaches a `Function` as a listener for `key` |
140
+ | `emit(key, [value])` | Function | emits to all connected sockets |
141
+ | `once(key, [value])` | Function | emits to the sockets and replace if exists a pending message with the same `key` |
142
+ | `broadcast(sender, key, [value])` | Function | emits to all connected sockets except sender |
143
+ | `broadcastOnce(sender, key, [value])` | Function | emits to the sockets except sender and replace if exists a pending message with the same `key` |
144
+ | `sockets` | Set | a Set() with all the current connected sockets |
145
+ | `http` | node server | the underlying http(s) server |
141
146
 
142
147
  </details>
143
148
 
@@ -151,7 +156,7 @@ import Server from 'angelia.io/server'
151
156
 
152
157
  class _ {
153
158
  connect(socket, request) {
154
- console.log(socket, data, callback)
159
+ console.log(socket, request)
155
160
  }
156
161
  }
157
162
  Server.on(_)
@@ -361,7 +366,6 @@ As in `socket.on('connect', () => console.log('connect happened!'))`
361
366
  | `connect` | **this happens only once** when we connect to the server, any future connection is a `reconnect` |
362
367
  | `reconnect` | if we were connected at least once, then any reconnection will dispatch this event instead of `connect` |
363
368
  | `disconnect` | when we disconnect from the server |
364
- | |
365
369
 
366
370
  </details>
367
371
 
package/server/Server.js CHANGED
@@ -17,6 +17,7 @@ class ServerSingleton {
17
17
 
18
18
  cert: '',
19
19
  key: '',
20
+ http: null,
20
21
 
21
22
  queue: [],
22
23
 
@@ -92,38 +93,42 @@ class ServerSingleton {
92
93
  maxMessageSize: +options.maxMessageSize > 0 ? +options.maxMessageSize : this.maxMessageSize,
93
94
  cert: options.cert || this.cert,
94
95
  key: options.key || this.key,
96
+ http: options.http || this.http,
95
97
 
96
98
  since: Date.now(),
97
99
  now: Date.now(),
100
+ timeout: +options.timeout >= 10000 ? +options.timeout : this.timeout,
98
101
  })
102
+ this.timeoutCheck = this.timeout / 2
99
103
 
100
104
  this.ensureFastProperties()
101
105
 
102
106
  // updates ping and checks for disconnections
103
- setInterval(this.ping, 30 * 1000)
107
+ setInterval(this.ping, this.timeoutCheck)
104
108
  setInterval(this.updateNow, 500)
105
109
 
106
110
  // fires the server
107
- let server
108
- function handle(req, res) {
109
- res.writeHead(200, { 'Content-Type': 'text/plain' })
110
- res.end()
111
- }
112
- if (this.cert && this.key) {
113
- let fs = require('fs')
114
- server = require('https').createServer(
115
- {
116
- cert: fs.readFileSync(this.cert), // fullchain.pem
117
- key: fs.readFileSync(this.key), // privkey.pem
118
- },
119
- handle,
120
- )
121
- } else {
122
- server = require('http').createServer(handle)
111
+ if (!this.http) {
112
+ function handle(req, res) {
113
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
114
+ res.end()
115
+ }
116
+ if (this.cert && this.key) {
117
+ let fs = require('fs')
118
+ this.http = require('https').createServer(
119
+ {
120
+ cert: fs.readFileSync(this.cert), // fullchain.pem
121
+ key: fs.readFileSync(this.key), // privkey.pem
122
+ },
123
+ handle,
124
+ )
125
+ } else {
126
+ this.http = require('http').createServer(handle)
127
+ }
123
128
  }
124
129
 
125
130
  let io = new this.WebSocket.Server({
126
- server: server,
131
+ server: this.http,
127
132
  perMessageDeflate: false,
128
133
  maxPayload: this.maxMessageSize * 1024 * 1024,
129
134
  clientTracking: false,
@@ -135,7 +140,7 @@ class ServerSingleton {
135
140
  io.on('connection', this.onconnect)
136
141
  io.on('error', this.onerror)
137
142
 
138
- server.listen(this.port, this.hostname || null)
143
+ this.http.listen(this.port, this.hostname || null)
139
144
 
140
145
  for (let m of ['RESTART', 'SIGINT', 'SIGTERM', 'SIGBREAK']) {
141
146
  process.on(m, () => {
@@ -156,8 +161,6 @@ class ServerSingleton {
156
161
 
157
162
  console.log('Socket Server Started Listening On Port ', this.port)
158
163
  this.events.listen && this.events.listen()
159
-
160
- return this
161
164
  }
162
165
 
163
166
  // count of connections
@@ -216,8 +219,6 @@ class ServerSingleton {
216
219
 
217
220
  // PRIVATE API
218
221
  onconnect(socket, request) {
219
- this.updateNow()
220
-
221
222
  socket = new this.Socket(socket, this)
222
223
 
223
224
  // set the ip, userAgent and params
@@ -304,8 +305,18 @@ class ServerSingleton {
304
305
  socket.timedout = true
305
306
  this.events.timeout && this.events.timeout(socket.proxy, delay)
306
307
  socket.io.terminate()
307
- } else {
308
- // ping
308
+ } else if (delay > this.timeoutCheck - 5000) {
309
+ /*
310
+ in an example:
311
+ if timeout is set to 60 seeconds
312
+ then we check for timed out sockets every 30 seconds
313
+ if the socket was last seen 29 seconds ago
314
+ then the next check will be in another 30 seconds
315
+ that means that if the socket doesnt sends any message
316
+ then the last seen will be 59 seconds the next time
317
+ this gives very little amount of time to check
318
+ for this reason we remove 5 seconds on the condition
319
+ */
309
320
  this.pingSocket(socket)
310
321
  }
311
322
  }
@@ -51,7 +51,7 @@ class Room {
51
51
  this[parent][remove](this)
52
52
 
53
53
  // dispatch leave
54
- this.onLeave && this.onLeave(socket)
54
+ // this.onLeave && this.onLeave(socket)
55
55
 
56
56
  // dispatch that the room has been deleted
57
57
  this.onDelete && this.onDelete(socket)