From 0fa2cc2590dbd8707520ffd9f9deb43a0b470f26 Mon Sep 17 00:00:00 2001 From: Daedalus11069 Date: Fri, 25 Dec 2015 19:48:58 -0800 Subject: [PATCH] Make async call of .disconnect() optional --- lib/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/index.js b/lib/index.js index 41d951b..160360c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,7 +34,23 @@ function noQsMethod(options) { var error = new UnauthorizedError(code, { message: (Object.prototype.toString.call(err) === '[object Object]' && err.message) ? err.message : err }); + var callback_timeout; + // If callback explicitely set to false, start timeout to disconnect socket + if (options.callback === false || typeof options.callback === "number") { + if (typeof options.callback === "number") { + if (options.callback < 0) { + // If callback is negative(invalid value), make it positive + options.callback = Math.abs(options.callback); + } + } + callback_timeout = setTimeout(function () { + socket.disconnect('unauthorized'); + }, (options.callback === false ? 0 : options.callback)); + } socket.emit('unauthorized', error, function() { + if (typeof options.callback === "number") { + clearTimeout(callback_timeout); + } socket.disconnect('unauthorized'); }); return; // stop logic, socket will be close on next tick