angelia.io 1.58.0 → 1.62.0

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/index.js CHANGED
@@ -1,219 +1,228 @@
1
1
  'use strict'
2
2
 
3
- const ClientWebWorker = `
4
- class ClientWebWorker {
5
- constructor() {
6
- Object.assign(this, {
7
- connid: this.generateId(),
3
+ const ClientWebWorker = `class ClientWebWorker {
4
+ constructor() {
5
+ Blob.prototype.toJSON = function(){
6
+ return {
7
+ b: new FileReaderSync().readAsDataURL(this)
8
+ }
9
+ }
10
+ ArrayBuffer.prototype.toJSON = function(){
11
+ return {
12
+ a: new FileReaderSync().readAsDataURL(this)
13
+ }
14
+ }
8
15
 
9
- connected: true,
10
- reconnect: true,
11
- isReconnect: false,
16
+ Object.assign(this, {
17
+ connid: this.generateId(),
12
18
 
13
- messages: [],
14
- buffered: [],
19
+ connected: true,
20
+ reconnect: true,
21
+ isReconnect: false,
15
22
 
16
- onopen: this.onopen.bind(this),
17
- onclose: this.onclose.bind(this),
18
- onerror: this.onerror.bind(this),
19
- onmessage: this.onmessage.bind(this),
23
+ messages: [],
24
+ buffered: [],
20
25
 
21
- postMessage: self.postMessage.bind(self),
22
- })
26
+ onopen: this.onopen.bind(this),
27
+ onclose: this.onclose.bind(this),
28
+ onerror: this.onerror.bind(this),
29
+ onmessage: this.onmessage.bind(this),
23
30
 
24
- self.onmessage = function(e) {
25
- switch (e.data[0]) {
26
- case 'connect': {
27
- this.connect(e.data[1])
28
- break
29
- }
30
- case 'disconnect': {
31
- this.disconnect(e.data[1])
32
- break
33
- }
34
- case 'emit': {
35
- this.emit(e.data[1])
36
- break
37
- }
38
- }
39
- }.bind(this)
40
- }
31
+ postMessage: self.postMessage.bind(self),
32
+ })
41
33
 
42
- connect(options) {
43
- if (
44
- this.reconnect &&
45
- (!this.io || this.io.readyState === WebSocket.CLOSED)
46
- ) {
47
- let url = new URL(options.url)
48
-
49
- // parms creation
50
- let params = options.params
51
- if (!params.connid) {
52
- params.connid = this.connid
34
+ self.onmessage = function (e) {
35
+ switch (e.data[0]) {
36
+ case 'emit': {
37
+ this.emit(e.data[1])
38
+ break
53
39
  }
54
- for (let [k, v] of url.searchParams.entries()) {
55
- if (!params.hasOwnProperty(k)) {
56
- params[k] = v
57
- }
40
+ case 'connect': {
41
+ this.connect(e.data[1])
42
+ break
58
43
  }
59
- url.search = Object.entries(params)
60
- .filter(([k, v]) => {
61
- return !(
62
- k === undefined ||
63
- k === null ||
64
- v === undefined ||
65
- v === null
66
- )
67
- })
68
- .map(([k, v]) => encodeURIComponent(k) + '=' + encodeURIComponent(v))
69
- .join('&')
70
-
71
- url = url.toString()
72
-
73
- // append buffered messages
74
- let oURL = url
75
- if (this.messages.length && !this.isReconnect) {
76
- url =
77
- (url.indexOf('?') === -1 ? url + '?' : url + '&') +
78
- 'angelia.io=' +
79
- encodeURIComponent(JSON.stringify(this.messages))
80
- if (url.length < 2048) {
81
- this.messages = []
82
- } else {
83
- url = oURL
84
- }
85
- } else if (this.messages.length && this.isReconnect) {
86
- for (let m of this.messages) {
87
- // dont buffer callbacks, they are gone for new connections
88
- if(m[0] !== '') this.buffered.push(m)
89
- }
90
- this.messages = []
44
+ case 'disconnect': {
45
+ this.disconnect(e.data[1])
46
+ break
91
47
  }
92
- this.io = new WebSocket(url)
93
- Object.assign(this.io, {
94
- onopen: this.onopen,
95
- onclose: this.onclose,
96
- onerror: this.onerror,
97
- onmessage: this.onmessage,
98
- })
99
48
  }
100
- }
101
-
102
- disconnect(noReconnect) {
103
- if (noReconnect) this.reconnect = false
49
+ }.bind(this)
50
+ }
104
51
 
105
- if (
106
- this.io &&
107
- this.io.readyState !== WebSocket.CLOSING &&
108
- this.io.readyState !== WebSocket.CLOSED
109
- ) {
110
- this.io.close()
52
+ connect(options) {
53
+ if (
54
+ this.reconnect &&
55
+ (!this.io || this.io.readyState === WebSocket.CLOSED)
56
+ ) {
57
+ let url = new URL(options.url)
58
+
59
+ // parms creation
60
+ let params = options.params
61
+ if (!params.connid) {
62
+ params.connid = this.connid
111
63
  }
64
+ for (let [k, v] of url.searchParams.entries()) {
65
+ if (!params.hasOwnProperty(k)) {
66
+ params[k] = v
67
+ }
68
+ }
69
+ url.search = Object.entries(params)
70
+ .filter(([k, v]) => {
71
+ return !(
72
+ k === undefined ||
73
+ k === null ||
74
+ v === undefined ||
75
+ v === null
76
+ )
77
+ })
78
+ .map(([k, v]) => encodeURIComponent(k) + '=' + encodeURIComponent(v))
79
+ .join('&')
80
+
81
+ url = url.toString()
82
+
83
+ // append buffered messages
84
+ let oURL = url
85
+ if (this.messages.length && !this.isReconnect) {
86
+ url =
87
+ (url.indexOf('?') === -1 ? url + '?' : url + '&') +
88
+ 'angelia.io=' +
89
+ encodeURIComponent(JSON.stringify(this.messages))
90
+ if (url.length < 2048) {
91
+ this.messages = []
92
+ } else {
93
+ url = oURL
94
+ }
95
+ } else if (this.messages.length && this.isReconnect) {
96
+ for (let m of this.messages) {
97
+ // dont buffer callbacks, they are gone for new connections
98
+ if (m[0] !== '') this.buffered.push(m)
99
+ }
100
+ this.messages = []
101
+ }
102
+ this.io = new WebSocket(url)
103
+ Object.assign(this.io, {
104
+ onopen: this.onopen,
105
+ onclose: this.onclose,
106
+ onerror: this.onerror,
107
+ onmessage: this.onmessage,
108
+ })
112
109
  }
110
+ }
113
111
 
114
- onopen() {
115
- this.connected = true
116
- this.postMessage(['connected', true])
112
+ disconnect(noReconnect) {
113
+ if (noReconnect) this.reconnect = false
114
+
115
+ if (
116
+ this.io &&
117
+ this.io.readyState !== WebSocket.CLOSING &&
118
+ this.io.readyState !== WebSocket.CLOSED
119
+ ) {
120
+ this.io.close()
121
+ }
122
+ }
117
123
 
118
- if (this.isReconnect) {
119
- this.postMessage(['dispatch', 'reconnect'])
120
- } else {
121
- this.isReconnect = true
122
- this.postMessage(['dispatch', 'connect'])
123
- }
124
+ onopen() {
125
+ this.connected = true
126
+ this.postMessage(['connected', true])
124
127
 
125
- this.sendMessages()
128
+ if (this.isReconnect) {
129
+ this.postMessage(['dispatch', 'reconnect'])
130
+ } else {
131
+ this.isReconnect = true
132
+ this.postMessage(['dispatch', 'connect'])
126
133
  }
127
- onclose(event) {
128
- switch (event.code) {
129
- // normal close
130
- case 1000:
131
- /*console.log('ws - normal close', event.code, event.reason)*/
132
- break
133
- // closed by client
134
- case 1005:
135
- /*console.log(
134
+
135
+ this.send()
136
+ }
137
+ onclose(event) {
138
+ switch (event.code) {
139
+ // normal close
140
+ case 1000:
141
+ /*console.log('ws - normal close', event.code, event.reason)*/
142
+ break
143
+ // closed by client
144
+ case 1005:
145
+ /*console.log(
136
146
  'ws - we called socket.disconnect()',
137
147
  event.code,
138
148
  event.reason,
139
149
  )*/
140
- break
141
- // closed by server or
142
- // connection never opened and failed to connect
143
- case 1006: {
144
- /*console.log(
150
+ break
151
+ // closed by server or
152
+ // connection never opened and failed to connect
153
+ case 1006: {
154
+ /*console.log(
145
155
  'ws - server killed the connection, or we failed to connect to server',
146
156
  event.code,
147
157
  event.reason,
148
158
  )*/
149
- break
150
- }
151
- default: {
152
- console.log('ws - unknown close', event.code, event.reason)
153
- break
154
- }
159
+ break
155
160
  }
156
-
157
- if (this.connected) {
158
- this.connected = false
159
- this.postMessage(['connected', false])
160
- this.postMessage(['dispatch', 'disconnect'])
161
+ default: {
162
+ console.log('ws - unknown close', event.code, event.reason)
163
+ break
161
164
  }
165
+ }
162
166
 
163
- if (this.reconnect) this.postMessage(['connect'])
167
+ if (this.connected) {
168
+ this.connected = false
169
+ this.postMessage(['connected', false])
170
+ this.postMessage(['dispatch', 'disconnect'])
164
171
  }
165
- // this happens when trying to connect while the server or
166
- // the internet connection is down
167
- onerror() {
168
- if (this.connected) {
169
- this.connected = false
170
- this.postMessage(['connected', false])
171
- this.postMessage(['dispatch', 'disconnect'])
172
- }
173
172
 
174
- if (this.reconnect) this.postMessage(['connect'])
173
+ if (this.reconnect) this.postMessage(['connect'])
174
+ }
175
+ // this happens when trying to connect while the server or
176
+ // the internet connection is down
177
+ onerror() {
178
+ if (this.connected) {
179
+ this.connected = false
180
+ this.postMessage(['connected', false])
181
+ this.postMessage(['dispatch', 'disconnect'])
175
182
  }
176
- onmessage(e) {
177
- if (e.data === '') {
178
- this.pong()
179
- } else {
180
- this.postMessage(['messages', JSON.parse(e.data)])
181
- }
183
+
184
+ if (this.reconnect) this.postMessage(['connect'])
185
+ }
186
+ onmessage(e) {
187
+ if (e.data === '') {
188
+ this.pong()
189
+ } else {
190
+ this.postMessage(['messages', JSON.parse(e.data)])
182
191
  }
183
- emit(messages) {
184
- for (let m of messages) {
185
- this.messages.push(m)
186
- }
187
- this.sendMessages()
192
+ }
193
+ emit(messages) {
194
+ for (let m of messages) {
195
+ this.messages.push(m)
188
196
  }
189
- sendMessages() {
190
- if (this.io && this.io.readyState === WebSocket.OPEN) {
191
- if (this.messages.length) {
192
- this.io.send(JSON.stringify(this.messages))
193
- this.messages = []
194
- }
195
- if (this.buffered.length) {
196
- this.io.send(JSON.stringify(this.buffered))
197
- this.buffered = []
198
- }
197
+ this.send()
198
+ }
199
+ send() {
200
+ if (this.io && this.io.readyState === WebSocket.OPEN) {
201
+ if (this.messages.length) {
202
+ let messages = this.messages
203
+ this.messages = []
204
+ this.io.send(JSON.stringify(messages))
199
205
  }
200
- }
201
- pong() {
202
- if (this.io && this.io.readyState === WebSocket.OPEN) {
203
- this.io.send('')
206
+ if (this.buffered.length) {
207
+ let buffered = this.buffered
208
+ this.buffered = []
209
+ this.io.send(JSON.stringify(buffered))
204
210
  }
205
211
  }
206
- generateId() {
207
- var id = ''
208
- while (!id) {
209
- id = Math.random()
210
- .toString(36)
211
- .substr(2, 10)
212
- }
213
- return id
212
+ }
213
+ pong() {
214
+ if (this.io && this.io.readyState === WebSocket.OPEN) {
215
+ this.io.send('')
214
216
  }
215
217
  }
216
- `
218
+ generateId() {
219
+ var id = ''
220
+ while (!id) {
221
+ id = Math.random().toString(36).substr(2, 10)
222
+ }
223
+ return id
224
+ }
225
+ }`
217
226
 
218
227
  class Client {
219
228
  constructor(options) {
@@ -255,7 +264,7 @@ class Client {
255
264
  postMessage: io.postMessage.bind(io),
256
265
  disconnect: this.disconnect.bind(this),
257
266
 
258
- sendMessages: this.sendMessages.bind(this),
267
+ send: this.send.bind(this),
259
268
  null: Object.create(null),
260
269
  })
261
270
 
@@ -287,6 +296,21 @@ class Client {
287
296
  this.postMessage(['disconnect', noReconnect])
288
297
  }
289
298
 
299
+ emit(k, v, c) {
300
+ if (!this.messages.length) {
301
+ Promise.resolve().then(this.send)
302
+ }
303
+ if (c) {
304
+ this.messages.push([k, v, this.callback(c)])
305
+ } else if (typeof v === 'function') {
306
+ this.messages.push([k, this.null, this.callback(v)])
307
+ } else if (v !== null && v !== undefined) {
308
+ this.messages.push([k, v])
309
+ } else {
310
+ this.messages.push([k])
311
+ }
312
+ }
313
+
290
314
  on(k, v) {
291
315
  if (k != '' && typeof v === 'function') {
292
316
  this.listeners[k] = this.listeners[k] || []
@@ -321,22 +345,7 @@ class Client {
321
345
  }
322
346
  }
323
347
 
324
- emit(k, v, c) {
325
- if (!this.messages.length) {
326
- Promise.resolve().then(this.sendMessages)
327
- }
328
- if (c) {
329
- this.messages.push([k, v, this.callback(c)])
330
- } else if (typeof v === 'function') {
331
- this.messages.push([k, this.null, this.callback(v)])
332
- } else if (v !== null && v !== undefined) {
333
- this.messages.push([k, v])
334
- } else {
335
- this.messages.push([k])
336
- }
337
- }
338
-
339
- // private API
348
+ // PRIVATE API
340
349
 
341
350
  callback(c) {
342
351
  let i = this.callbacks.length
@@ -382,10 +391,11 @@ class Client {
382
391
  }
383
392
  }
384
393
  }
385
- sendMessages() {
394
+ send() {
386
395
  if (this.messages.length) {
387
- this.postMessage(['emit', this.messages])
396
+ let messages = this.messages
388
397
  this.messages = []
398
+ this.postMessage(['emit', messages])
389
399
  }
390
400
  }
391
401
 
@@ -409,6 +419,13 @@ class Client {
409
419
  }
410
420
  }
411
421
  }
422
+ decode(s){
423
+ if (s.b) {
424
+ return fetch(s.b).then(r => r.blob())
425
+ } else if(s.a) {
426
+ return fetch(s.a).then(r => r.arrayBuffer())
427
+ }
428
+ }
412
429
  }
413
430
 
414
431
  ;(function (root) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "1.58.0",
3
+ "version": "1.62.0",
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": {
@@ -25,7 +25,7 @@
25
25
  "ws"
26
26
  ],
27
27
  "author": "@titoBouzout @boredofnames",
28
- "license": "MIT",
28
+ "license": "GPL",
29
29
  "dependencies": {
30
30
  "bufferutil": "^4.0.3",
31
31
  "is-class": "^0.0.9",
@@ -33,5 +33,67 @@
33
33
  "to-fast-properties": "^3.0.1",
34
34
  "utf-8-validate": "^5.0.4",
35
35
  "ws": "^7.4.3"
36
+ },
37
+ "prettier": {
38
+ "printWidth": 100,
39
+ "useTabs": true,
40
+ "semi": false,
41
+ "singleQuote": true,
42
+ "jsxSingleQuote": false,
43
+ "trailingComma": "all",
44
+ "bracketSpacing": true,
45
+ "jsxBracketSameLine": false,
46
+ "arrowParens": "avoid",
47
+ "quoteProps": "preserve",
48
+ "proseWrap": "preserve"
49
+ },
50
+ "eslintConfig": {
51
+ "env": {
52
+ "browser": true,
53
+ "node": true,
54
+ "es6": true
55
+ },
56
+ "parser": "babel-eslint",
57
+ "extends": "eslint:recommended",
58
+ "parserOptions": {
59
+ "ecmaFeatures": {
60
+ "experimentalObjectRestSpread": true,
61
+ "jsx": true,
62
+ "impliedStrict": true,
63
+ "es6": true
64
+ },
65
+ "es6": true,
66
+ "ecmaVersion": 6,
67
+ "sourceType": "module",
68
+ "allowImportExportEverywhere": false,
69
+ "codeFrame": false
70
+ },
71
+ "plugins": [
72
+ "react"
73
+ ],
74
+ "rules": {
75
+ "comma-dangle": "off",
76
+ "indent": "off",
77
+ "linebreak-style": [
78
+ "error",
79
+ "unix"
80
+ ],
81
+ "no-cond-assign": "off",
82
+ "no-console": "off",
83
+ "no-empty": "off",
84
+ "no-extra-boolean-cast": "off",
85
+ "no-extra-semi": "off",
86
+ "no-fallthrough": "off",
87
+ "no-func-assign": "off",
88
+ "no-inner-declarations": "off",
89
+ "no-mixed-spaces-and-tabs": "off",
90
+ "no-redeclare": "off",
91
+ "no-tabs": "off",
92
+ "no-undef": "off",
93
+ "no-global-assign": "off",
94
+ "no-unreachable": "off",
95
+ "no-unused-vars": "off",
96
+ "strict": "off"
97
+ }
36
98
  }
37
99
  }
@@ -1,4 +1,3 @@
1
- cd ..
2
1
 
3
2
  git add --all
4
3
  git commit -m "update"
@@ -12,4 +11,3 @@ git push --all --prune
12
11
 
13
12
  call npm publish
14
13
 
15
- exit
package/readme.md CHANGED
@@ -269,11 +269,11 @@ Server.on(function connect(socket, request) {
269
269
 
270
270
  // listen via the properties of an object to all the functions of it
271
271
  Server.on({
272
- connect: function(socket, request) {
272
+ connect: function (socket, request) {
273
273
  console.log('connect in Object')
274
274
  this.works()
275
275
  },
276
- works: function() {
276
+ works: function () {
277
277
  console.log('this works yep')
278
278
  },
279
279
  })
@@ -376,6 +376,7 @@ The client API is similar to regular event handling
376
376
  | `on(key, callback)` | Function | listens for an event, returns an `off` function to stop listening |
377
377
  | `off(key, callback)` | Function | turns off listening for an event |
378
378
  | `emit(key, [value, callback])` | Function | emits data to the server |
379
+ | `decode(data)` | Function | for decoding binary data, returns a promise |
379
380
 
380
381
  #### List of Predefined Events
381
382
 
package/server/Server.js CHANGED
@@ -106,8 +106,8 @@ class ServerSingleton {
106
106
 
107
107
  this.ensureFastProperties()
108
108
 
109
- // ping, updates ping and checks for disconnections
110
- setInterval(this.ping, 30 * 1000) // twice a minute more than enough
109
+ // updates ping and checks for disconnections
110
+ setInterval(this.ping, 30 * 1000)
111
111
  setInterval(this.updateNow, 500)
112
112
 
113
113
  // fires the server
@@ -160,14 +160,13 @@ class ServerSingleton {
160
160
 
161
161
  return this
162
162
  }
163
- // count of connections
164
163
 
164
+ // count of connections
165
165
  get connections() {
166
166
  return this.sockets.size
167
167
  }
168
168
 
169
169
  // listen for an event
170
-
171
170
  on(k, cb) {
172
171
  let instance = this.listeners.on(k, cb)
173
172
  Object.defineProperties(instance, {
@@ -183,7 +182,6 @@ class ServerSingleton {
183
182
  }
184
183
 
185
184
  // emits to everyone connected to the server
186
-
187
185
  emit(k, v) {
188
186
  let d = [k, v]
189
187
  for (let socket of this.sockets) {
@@ -216,8 +214,8 @@ class ServerSingleton {
216
214
  }
217
215
  return this._track(path)
218
216
  }
219
- // PRIVATE API
220
217
 
218
+ // PRIVATE API
221
219
  onconnect(socket, request) {
222
220
  this.updateNow()
223
221
 
@@ -262,21 +260,18 @@ class ServerSingleton {
262
260
  }
263
261
 
264
262
  // queue
265
-
266
263
  nextQueue(socket) {
267
264
  if (!this.queue.length) {
268
265
  process.nextTick(this.processQueue)
269
266
  }
270
267
  this.queue.push(socket)
271
268
  }
272
-
273
269
  processQueue() {
274
270
  let queue = this.queue
275
271
  this.queue = []
276
272
  for (let socket of queue) {
277
273
  socket.processQueue()
278
274
  }
279
- // clear cache
280
275
  this.cache = Object.create(null)
281
276
  }
282
277
 
@@ -325,7 +320,6 @@ class ServerSingleton {
325
320
  this.updateNow()
326
321
  socket.contacted = this.now
327
322
  if (socket.io.readyState === 1) {
328
- // socket.io.send('')
329
323
  for (let m of this.pingData) socket.io._socket.write(m)
330
324
  }
331
325
  }
@@ -391,7 +385,6 @@ class ServerSingleton {
391
385
 
392
386
  // stats
393
387
  connections: this.connections,
394
-
395
388
  served: this.served,
396
389
  bytesSent: this.bytesSent,
397
390
  bytesReceived: this.bytesReceived,
@@ -412,8 +405,10 @@ class ServerSingleton {
412
405
  once: this.once,
413
406
  broadcast: this.broadcast,
414
407
  broadcastOnce: this.broadcastOnce,
408
+
415
409
  // data
416
410
  // sockets: this.sockets,
411
+
417
412
  // rooms
418
413
  track: this.track,
419
414
  rooms: this.rooms,
package/server/Socket.js CHANGED
@@ -23,7 +23,7 @@ class Socket {
23
23
 
24
24
  since: server.now,
25
25
  seen: server.now,
26
- contacted: server.now, // for ping
26
+ contacted: server.now,
27
27
  ping: 0,
28
28
  timedout: false,
29
29
 
@@ -40,43 +40,31 @@ class Socket {
40
40
 
41
41
  null: Object.create(null),
42
42
  })
43
- this.toJSON = this.inspect
43
+ this.toJSON = this[inspect] = this.inspect
44
44
 
45
45
  Object.assign(socket, {
46
46
  [inspect]: this.inspect,
47
47
  toJSON: this.inspect,
48
48
  })
49
49
  }
50
- callback(c) {
51
- let i = this.callbacks.length
52
- this.callbacks[i] = c
53
- return i
54
- }
55
- oncallback(d, m) {
56
- if (!this.callbacks[d[0]]) {
57
- console.log(m, this)
58
- } else {
59
- this.callbacks[d[0]](...d[1])
60
- this.callbacks[d[0]] = null
61
- }
62
- }
63
50
  emit(k, v, cb) {
64
51
  if (!this.messages.length) {
65
52
  this.server.nextQueue(this)
66
53
  }
67
- if (typeof v === 'function') {
68
- this.messages.push([k, this.null, this.callback(v)])
69
- } else if (cb) {
54
+
55
+ if (cb) {
70
56
  this.messages.push([k, v, this.callback(cb)])
57
+ } else if (typeof v === 'function') {
58
+ this.messages.push([k, this.null, this.callback(v)])
71
59
  } else {
72
- this.messages.push(typeof k !== 'string' ? k : [k, v])
60
+ this.messages.push(typeof k === 'object' ? k : [k, v])
73
61
  }
74
62
  }
75
63
  once(k, v) {
76
64
  if (!this.messages.length) {
77
65
  this.emit(k, v)
78
66
  } else {
79
- if (typeof k !== 'string') {
67
+ if (typeof k === 'object') {
80
68
  for (let m of this.messages) {
81
69
  if (m[0] === k[0]) {
82
70
  m[1] = k[1]
@@ -94,14 +82,14 @@ class Socket {
94
82
  this.emit(k, v)
95
83
  }
96
84
  }
97
-
98
85
  disconnect(noReconnect) {
99
86
  if (noReconnect) {
100
87
  for (let m of this.server.disconnectData) this.io._socket.write(m)
101
88
  }
102
89
  this.io.close()
103
90
  }
104
- // private API
91
+
92
+ // PRIVATE API
105
93
  listen() {
106
94
  this.io.on('close', this.onclose)
107
95
  this.io.on('error', this.onerror)
@@ -121,9 +109,34 @@ class Socket {
121
109
  this.server.socketErrors++
122
110
  console.error('Socket.onerror', err, this.inspect())
123
111
  }
112
+
113
+ // callbacks
114
+ callback(c) {
115
+ let i = this.callbacks.length
116
+ this.callbacks[i] = c
117
+ return i
118
+ }
119
+ oncallback(d, m) {
120
+ if (!this.callbacks[d[0]]) {
121
+ console.log('oncallback doesnt exits', this, m)
122
+ } else {
123
+ this.callbacks[d[0]](...d[1])
124
+ this.callbacks[d[0]] = null
125
+ }
126
+ }
124
127
  doreply(k, ...v) {
125
128
  this.emit('', [k, v])
126
129
  }
130
+
131
+ // messages
132
+ parse(o) {
133
+ try {
134
+ return JSON.parse(o)
135
+ } catch (e) {
136
+ console.log('parser error', this, o)
137
+ return false
138
+ }
139
+ }
127
140
  onmessage(e) {
128
141
  if (e === '') {
129
142
  this.server.pong(this)
@@ -133,14 +146,9 @@ class Socket {
133
146
  this.server.bytesReceived += e.length
134
147
  this.bytesReceived += e.length
135
148
 
136
- try {
137
- var messages = JSON.parse(e)
138
- } catch (m) {
139
- console.log(e, m)
140
- return
141
- }
149
+ let messages = this.parse(e)
142
150
 
143
- if (Array.isArray(messages)) {
151
+ if (messages && Array.isArray(messages)) {
144
152
  this.server.messagesReceived += messages.length
145
153
  this.messagesReceived += messages.length
146
154
 
@@ -164,31 +172,30 @@ class Socket {
164
172
  } else {
165
173
  this.server.messagesGarbage++
166
174
  this.server.events.garbage &&
167
- this.server.events.garbage(this.proxy, messages)
175
+ this.server.events.garbage(this.proxy, messages || e)
168
176
  }
169
177
  }
170
178
  }
171
179
  processQueue() {
172
180
  if (this.io.readyState === 1) {
173
- // regular
174
181
  if (this.messages.length) {
182
+ let messages = this.messages
183
+ this.messages = []
184
+
175
185
  this.server.events.outgoing &&
176
- this.server.events.outgoing(this.proxy, this.messages)
186
+ this.server.events.outgoing(this.proxy, messages)
177
187
 
178
- this.server.messagesSent += this.messages.length
179
- this.messagesSent += this.messages.length
188
+ this.server.messagesSent += messages.length
189
+ this.messagesSent += messages.length
190
+
191
+ messages = this.server.cacheMessages(messages, this)
180
192
 
181
- let messages = this.server.cacheMessages(this.messages, this)
182
- // this.io.send(messages)
183
193
  for (let m of messages) this.io._socket.write(m)
184
194
  }
185
195
  }
186
196
  this.messages = []
187
197
  }
188
198
 
189
- [inspect]() {
190
- return Object.assign({}, this.toJSON ? this.toJSON() : {}, this.inspect())
191
- }
192
199
  inspect() {
193
200
  return {
194
201
  readyState: this.io.readyState,
package/.eslintrc DELETED
@@ -1,44 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "node": true,
5
- "es6": true
6
- },
7
- "parser": "babel-eslint",
8
- "extends": "eslint:recommended",
9
- "parserOptions": {
10
- "ecmaFeatures": {
11
- "experimentalObjectRestSpread": true,
12
- "jsx": true,
13
- "impliedStrict": true,
14
- "es6": true
15
- },
16
- "es6": true,
17
- "ecmaVersion": 6,
18
- "sourceType": "module",
19
- "allowImportExportEverywhere": false,
20
- "codeFrame": false
21
- },
22
- "plugins": ["react"],
23
- "rules": {
24
- "comma-dangle": "off",
25
- "indent": "off",
26
- "linebreak-style": ["error", "unix"],
27
- "no-cond-assign": "off",
28
- "no-console": "off",
29
- "no-empty": "off",
30
- "no-extra-boolean-cast": "off",
31
- "no-extra-semi": "off",
32
- "no-fallthrough": "off",
33
- "no-func-assign": "off",
34
- "no-inner-declarations": "off",
35
- "no-mixed-spaces-and-tabs": "off",
36
- "no-prototype-builtins": "off",
37
- "no-redeclare": "off",
38
- "no-tabs": "off",
39
- "no-undef": "off",
40
- "no-unreachable": "off",
41
- "no-unused-vars": "off",
42
- "strict": "off"
43
- }
44
- }
package/.prettierrc.js DELETED
@@ -1,15 +0,0 @@
1
- module.exports = {
2
- 'printWidth': 80,
3
- 'useTabs': true,
4
- 'semi': false,
5
-
6
- 'singleQuote': true,
7
- 'jsxSingleQuote': false,
8
- 'trailingComma': 'all',
9
- 'bracketSpacing': true,
10
- 'jsxBracketSameLine': false,
11
- 'arrowParens': 'avoid',
12
- 'quoteProps': 'preserve',
13
-
14
- 'proseWrap': 'preserve',
15
- }
package/@dev/@link.bat DELETED
@@ -1,14 +0,0 @@
1
-
2
- cd ..
3
- CMD /C npm link
4
-
5
- cd ../client/
6
- CMD /C npm link angelia.io
7
-
8
- cd ../server/
9
- CMD /C npm link angelia.io
10
-
11
- cd ../angelia.io
12
- CMD /C npm install
13
-
14
- exit