angelia.io 2.4.70 → 2.4.71

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.70",
3
+ "version": "2.4.71",
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
@@ -1,6 +1,6 @@
1
1
  # angelia.io
2
2
 
3
- WebSockets Server and Client API for node.js and the browser, with rooms support.
3
+ WebSockets Server and Client API for node.js and the browser, (with rooms support in development).
4
4
 
5
5
  The goal of this project is to provide a developer friendly API that just works™.
6
6
 
@@ -12,18 +12,24 @@ If you fancy for Client side a regular JavaScript file, then use https://github.
12
12
 
13
13
  ## Simple Example
14
14
 
15
- ### Server (Node.js)
16
-
17
15
  ```JavaScript
18
16
  // server.js (node.js)
19
17
  import Server from 'angelia.io/server';
20
18
 
21
- class Connection {
22
- async listen() {
23
- console.log('-'.repeat(60));
24
- console.log('Server started listening on port ' + this.server.port);
25
- console.log(this.server);
19
+ class FancyChat {
20
+ async typing(socket, data, callback) {
21
+ console.log('Im', data ? ' typing' : ' not typing')
26
22
  }
23
+ async theMessage(socket, data, callback) {
24
+ console.log('the message is', data, socket)
25
+ socket.emit('gotIt', 'thanks')
26
+ callback('yes Im sure')
27
+ }
28
+ }
29
+
30
+ Server.on(FancyChat);
31
+
32
+ class Connection {
27
33
  async connect(socket, request) {
28
34
  console.log('socket connected', socket)
29
35
  }
@@ -33,42 +39,30 @@ class Connection {
33
39
  }
34
40
  Server.on(Connection);
35
41
 
36
- class FancyChat {
37
- async chatMessage(socket, message, callback) {
38
- console.log('socket sent', message)
39
- socket.emit('chatMessage', 'Hi client nice to meet you')
40
- callback('yes, got it')
41
- }
42
- }
43
- Server.on(FancyChat);
44
-
45
- const server = new Server.listen({
42
+ Server.listen({
46
43
  port: 3001,
47
44
  });
48
- ```
49
-
50
- ### Client (Browser)
51
45
 
52
- ```JavaScript
53
46
  // index.js (browser)
54
47
  import Client from 'angelia.io/client';
55
48
 
56
- const socket = new Client({
57
- url: 'ws://localhost:3001',
58
- });
49
+ const socket = new Client('ws://localhost:3001');
59
50
 
60
- socket.on('chatMessage', (message) => {
61
- console.log('Server sent', message)
62
- })
51
+ socket.emit('typing', true)
63
52
 
64
- socket.emit('chatMessage', 'Hi there server, Im client', (data) =>{
65
- console.log('you got it?', data)
53
+ setTimeout(() => {
54
+ socket.emit('typing', false)
55
+ socket.emit('theMessage', 'hi there!', (data) =>{
56
+ console.log('you sure?', data)
57
+ })
58
+ }, 10000)
59
+
60
+ socket.on('gotIt', (message) => {
61
+ console.log('Server got it yey', message)
66
62
  })
67
63
  ```
68
64
 
69
- ## Server (Node.js)
70
-
71
- ### Options
65
+ ## Server Documentation (Node.js)
72
66
 
73
67
  A call to `Server.listen` starts the server. `Server` is a singleton and can only have running 1 server.
74
68
 
@@ -76,7 +70,7 @@ A call to `Server.listen` starts the server. `Server` is a singleton and can onl
76
70
  // server.js (node.js)
77
71
  import Server from 'angelia.io/server'
78
72
 
79
- const server = new Server.listen({
73
+ Server.listen({
80
74
  hostname: 'localhost',
81
75
  port: 3001,
82
76
  maxMessageSize: 5,
@@ -85,13 +79,14 @@ const server = new Server.listen({
85
79
  })
86
80
  ```
87
81
 
82
+ ### `Server` Options
83
+
88
84
  | name | kind | default | description |
89
85
  | -------------------- | ------- | ------- | --------------------------------------------------- |
90
86
  | `hostname` | String | '' | the hostname if any |
91
87
  | `port` | Number | 3001 | the port to use for this server |
92
88
  | `maxMessageSize` | Number | 5 | max size in mb of a message received |
93
89
  | `skipUTF8Validation` | Boolean | false | allows to skip utf8 validation |
94
- | `dontConnect` | Boolean | false | allows to instance the socket without connecting |
95
90
  | `cert` | String | '' | path to the cert file for using https fullchain.pem |
96
91
  | `key` | String | '' | path to the key file for using https privkey.pem |
97
92
 
@@ -110,12 +105,15 @@ class _ {
110
105
  }
111
106
  Server.on(_)
112
107
 
113
- const server = new Server.listen({
108
+ Server.listen({
114
109
  port: 3001,
115
110
  })
116
111
  ```
117
112
 
118
- #### Server Properties
113
+ #### List of `Server` Object Properties
114
+
115
+ <details>
116
+ <summary><b>Expand</b></summary>
119
117
 
120
118
  | signature | kind | description |
121
119
  | ------------------------------------- | -------- | ---------------------------------------------------------------------------------------------- |
@@ -141,7 +139,9 @@ const server = new Server.listen({
141
139
  | `broadcastOnce(sender, key, [value])` | Function | emits to the sockets except sender and replace if exists a pending message with the same `key` |
142
140
  | `sockets` | Set | a Set() with all the current connected sockets |
143
141
 
144
- ### Socket Object
142
+ </details>
143
+
144
+ ### `Socket` Object
145
145
 
146
146
  The `socket` object is given to you by a listener
147
147
 
@@ -156,12 +156,15 @@ class _ {
156
156
  }
157
157
  Server.on(_)
158
158
 
159
- new Server.listen({
159
+ Server.listen({
160
160
  port: 3001,
161
161
  })
162
162
  ```
163
163
 
164
- #### Socket Properties
164
+ #### List of `Socket` Object Properties
165
+
166
+ <details>
167
+ <summary><b>Expand</b></summary>
165
168
 
166
169
  | signature | kind | description |
167
170
  | --------------------------- | -------- | ----------------------------------------------------------------------------- |
@@ -182,72 +185,23 @@ new Server.listen({
182
185
  | `once(key, [value])` | Function | replace if exists a pending message with the same `key` from emit queue |
183
186
  | `disconnect([noReconnect])` | Function | disconnects the socket from the server, pass `true` to prevent re-connections |
184
187
 
188
+ </details>
189
+
185
190
  ### Listeners
186
191
 
187
192
  To listen for a client message/event you may do the familiar way `Server.on('connect', (socket) => {console.log('socket connected!', socket)})`
188
193
 
189
194
  However, to ease organization and development you may listen to an event by creating a class with any name, and give to methods the name of the things you want to listen to. You then add your class to the listeners as `Server.on(MyClass);` and you are done.
190
195
 
191
- On user defined listeners, the listener receives three things as sent by the client: `socket`, `data` and a `callback`; Example `class FancyChat { async typing(socket, data, callback) {console.log(socket, data);callback('got it')}}`.
192
-
193
- If you are building a chat you may write something like this
194
-
195
- ```JavaScript
196
- // server.js (node.js)
197
- import Server from 'angelia.io/server';
198
-
199
- class FancyChat {
200
- async typing(socket, data, callback) {
201
- console.log('Im', data ? ' typing' : ' not typing')
202
- }
203
- async theMessage(socket, data, callback) {
204
- console.log('the message is', data, socket)
205
- socket.emit('gotIt', 'thanks')
206
- callback('yes Im sure')
207
- }
208
- }
209
-
210
- Server.on(FancyChat);
211
-
212
- class Connection {
213
- async connect(socket, request) {
214
- console.log('socket connected', socket)
215
- }
216
- async disconnect(socket, code, message) {
217
- console.log('socket disconnected', code, message, socket);
218
- }
219
- }
220
- Server.on(Connection);
221
-
222
- const server = new Server.listen({
223
- port: 3001,
224
- });
225
-
226
- // index.js (browser)
227
- import Client from 'angelia.io/client';
228
-
229
- const socket = new Client({
230
- url: 'ws://localhost:3001',
231
- });
232
-
233
- socket.emit('typing', true)
234
-
235
- setTimeout(() => {
236
- socket.emit('typing', false)
237
- socket.emit('theMessage', 'hi there!', (data) =>{
238
- console.log('you sure?', data)
239
- })
240
- }, 10000)
241
-
242
- socket.on('gotIt', (message) => {
243
- console.log('Server got it yey', message)
244
- })
245
- ```
196
+ On user defined listeners, the listener receives three things as sent by the client: `socket`, `data` and a `callback`; Example `class FancyChat { async typing(socket, data, callback) {console.log(socket, data);}}`.
246
197
 
247
198
  #### Syntax For Listeners
248
199
 
249
200
  Listeners have the following alternative syntax if you feel like
250
201
 
202
+ <details>
203
+ <summary><b>Expand</b></summary>
204
+
251
205
  ```javascript
252
206
  // server.js (node.js)
253
207
  import Server from 'angelia.io/server'
@@ -290,12 +244,14 @@ Server.on('connect', function fancyConnect(socket, request) {
290
244
  onsole.log('connect in named function')
291
245
  })
292
246
 
293
- const server = new Server.listen({
247
+ Server.listen({
294
248
  port: 3001,
295
249
  })
296
250
  ```
297
251
 
298
- ### Predefined Events
252
+ </details>
253
+
254
+ ### Predefined `Server` Events
299
255
 
300
256
  There's a bunch of handy predefined events dispatched whenever you add listeners for them.
301
257
 
@@ -315,12 +271,15 @@ class _ {
315
271
 
316
272
  Server.on(_);
317
273
 
318
- const server = new Server.listen({
274
+ Server.listen({
319
275
  port: 3001,
320
276
  });
321
277
  ```
322
278
 
323
- #### List of Predefined Events
279
+ #### List of Predefined `Server` Events
280
+
281
+ <details>
282
+ <summary><b>Expand</b></summary>
324
283
 
325
284
  | signature | description |
326
285
  | ----------------------------------- | ----------------------------------------------------------------------------------------- |
@@ -341,16 +300,20 @@ const server = new Server.listen({
341
300
  | `events` | reference to event dispatcher, ex: `this.events.typing()` will dispatch the `typing` event to anyone listening to it |
342
301
  | `classes` | reference to all functions that have been attached as listeners , ex: `this.classes.MyFancyChat.typing` |
343
302
 
303
+ </details>
304
+
344
305
  ## Client API (Browser)
345
306
 
346
- ### Options
307
+ ### `Client` Options
347
308
 
348
309
  Configurable options used by the constructor
349
310
 
350
311
  ```javascript
351
312
  const socket = new Client({
352
313
  url: 'ws://localhost:3001',
353
- params: function (){ return { fast: 'data', test: 'a space' })
314
+ params: function () {
315
+ return { fast: 'data', test: 'a space' }
316
+ },
354
317
  })
355
318
  ```
356
319
 
@@ -359,6 +322,7 @@ const socket = new Client({
359
322
  | `url` | string | 'ws(s)://\${window.location.hostname}:3001' | url of the socket server, example 'ws://localhost:3001' |
360
323
  | `params` | Function | {} | to send data while connecting, accesible via `socket.params` server side |
361
324
  | `longLiveFlash` | boolean | false | browsers throw when calling swf functions via ExternalInterface after events like WebSocket.onmesssage; setting this to `true` fix it by dispatching them in a setTimeout |
325
+ | `dontConnect` | Boolean | false | allows to instance the socket without connecting |
362
326
 
363
327
  You may also do like this if you don't need any option
364
328
 
@@ -366,10 +330,13 @@ You may also do like this if you don't need any option
366
330
  const socket = new Client('ws://localhost:3001')
367
331
  ```
368
332
 
369
- ### API
333
+ ### `Client` API
370
334
 
371
335
  The client API is similar to regular event handling
372
336
 
337
+ <details>
338
+ <summary><b>Expand</b></summary>
339
+
373
340
  | signature | kind | description |
374
341
  | ------------------------------ | -------- | ------------------------------------------------------------------ |
375
342
  | `connected` | Boolean | `true` when the socket is connected else `false` |
@@ -380,15 +347,23 @@ The client API is similar to regular event handling
380
347
  | `emit(key, [value, callback])` | Function | emits data to the server |
381
348
  | `decode(data)` | Function | for decoding binary data, returns a promise |
382
349
 
383
- #### List of Predefined Events
350
+ </details>
351
+
352
+ #### List of Predefined `Client` Events
384
353
 
385
354
  As in `socket.on('connect', () => console.log('connect happened!'))`
386
355
 
356
+ <details>
357
+ <summary><b>Expand</b></summary>
358
+
387
359
  | signature | description |
388
360
  | ------------ | ------------------------------------------------------------------------------------------------------- |
389
361
  | `connect` | **this happens only once** when we connect to the server, any future connection is a `reconnect` |
390
362
  | `reconnect` | if we were connected at least once, then any reconnection will dispatch this event instead of `connect` |
391
363
  | `disconnect` | when we disconnect from the server |
364
+ | |
365
+
366
+ </details>
392
367
 
393
368
  ## Authors
394
369
 
@@ -9,6 +9,10 @@ class Room {
9
9
  has(socket) {
10
10
  return this.sockets.has(socket)
11
11
  }
12
+ get size() {
13
+ return this.sockets.size
14
+ }
15
+ // private
12
16
  [join](roomList, socket) {
13
17
  // add to list of room list
14
18
  if (!this[parent]) {
@@ -14,7 +14,9 @@ class Rooms {
14
14
  get(id) {
15
15
  return this.roomsById.get(id)
16
16
  }
17
-
17
+ get size() {
18
+ return this.rooms.size
19
+ }
18
20
  // private
19
21
  [add](room) {
20
22
  if (!this.rooms.has(room)) {