ws-events-and-middlewares
Advanced tools
Comparing version 1.0.2 to 1.0.3
29
index.js
@@ -1,2 +0,3 @@ | ||
var Emitter = require('component-emitter') | ||
const Emitter = require('component-emitter') | ||
const uuidv4 = require('uuid/v4') | ||
@@ -10,2 +11,3 @@ callbackCounter = 0 | ||
let callbacks = {} | ||
let clients = {} | ||
@@ -58,12 +60,27 @@ module.exports = function wsEvents (sock, middlewares = []) { | ||
function onopen () { | ||
// Generamos un id aleatorio | ||
let found = false | ||
while(!found) { | ||
const id = uuidv4() | ||
// Si no existe ningun cliente con este id lo guardamos | ||
if (!clients[id]) { | ||
found = true | ||
sock.id = id | ||
clients[id] = sock // Guardamos el cliente en 'la base de datos' | ||
} | ||
} | ||
// Procesamos todos los eventos pendientes emitidos antes de estar realemnte conectados | ||
onopenHandlers.forEach(function (fn) { | ||
fn() | ||
}) | ||
onopenHandlers = [] | ||
onopenHandlers = [] // Limpiamos los eventos pendientes | ||
} | ||
function whenOpen (fn) { | ||
// Comprobamos si estamos realmente conectados y ejecutamos la funcion dada | ||
if (sock.readyState === sock.constructor.OPEN) { | ||
fn() | ||
} else { | ||
// Si no estamos realmente conectados guardamos la funcion a ejecutar para lanzarla cuadno nos volvamos a conectar | ||
onopenHandlers.push(fn) | ||
@@ -73,6 +90,6 @@ } | ||
function onclose (e) { | ||
console.log('Onclose Arguments:') | ||
console.log(arguments) | ||
listeners.emit('close', e) | ||
function onclose (event) { | ||
// Eliminamos el cliente de 'la base de datos' | ||
delete clients[sock.id] | ||
listeners.emit('close', event) | ||
} | ||
@@ -79,0 +96,0 @@ |
{ | ||
"name": "ws-events-and-middlewares", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "ws module wrapper to provide events", | ||
@@ -27,4 +27,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"component-emitter": "^1.2.1" | ||
"component-emitter": "^1.2.1", | ||
"uuid": "^3.3.2" | ||
} | ||
} |
@@ -69,7 +69,21 @@ # ws-events-and-middlewares | ||
## Construyendo el servidor | ||
## Instalando ws-events-and-middlewares | ||
```js | ||
npm install ws-events-and-middlewares --save | ||
``` | ||
o | ||
```js | ||
yarn install ws-events-and-middlewares --save | ||
``` | ||
## Construyendo el servidor con eventos | ||
## Construyendo el cliente | ||
## Construyendo el servidor con eventos y middlewares | ||
### TODO list | ||
@@ -76,0 +90,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8347
116
92
2
+ Addeduuid@^3.3.2
+ Addeduuid@3.4.0(transitive)