From 6901a4ef2d3323f7523e6baf4c342b6ddd4744f9 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 28 Apr 2017 15:02:44 -0600 Subject: [PATCH] added handling for 'hello' from the server --- wsclient.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/wsclient.js b/wsclient.js index 0961437..921fea2 100644 --- a/wsclient.js +++ b/wsclient.js @@ -98,6 +98,18 @@ function run(copts) { }); } + function sendAllTokens() { + tokens.forEach(function (jwtoken) { + sendCommand('add_token', jwtoken) + .catch(function (err) { + console.error('failed re-adding token', jwtoken, 'after reconnect', err); + // Not sure if we should do something like remove the token here. It worked + // once or it shouldn't have stayed in the list, so it's less certain why + // it would have failed here. + }); + }); + } + var packerHandlers = { oncontrol: function (opts) { var cmd, err; @@ -119,8 +131,22 @@ function run(copts) { return; } - // TODO: handle a "hello" message that let's us know we're authenticated. - err = { message: 'unknown command '+cmd[1], code: 'E_UNKNOWN_COMMAND' }; + if (cmd[0] === 0) { + console.warn('received dis-associated error from server', cmd[1]); + return; + } + + if (cmd[1] === 'hello') { + // We only get the 'hello' event after the token has been validated + authenticated = true; + sendAllTokens(); + // TODO: handle the versions and commands provided by 'hello' - isn't super important + // yet since there is only one version and set up commands. + err = null; + } + else { + err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }; + } wsHandlers.sendMessage(Packer.pack(null, [-cmd[0], err], 'control')); } @@ -134,8 +160,6 @@ function run(copts) { var str; var m; - authenticated = true; - if (localclients[cid]) { //console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength); localclients[cid].write(opts.data); @@ -268,16 +292,6 @@ function run(copts) { console.info("[open] connected to '" + copts.stunneld + "'"); wsHandlers.refreshTimeout(); timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout); - - tokens.forEach(function (jwtoken) { - sendCommand('add_token', jwtoken) - .catch(function (err) { - console.error('failed re-adding token', jwtoken, 'after reconnect', err); - // Not sure if we should do something like remove the token here. It worked - // once or it shouldn't have stayed in the list, so it's less certain why - // it would have failed here. - }); - }); } , onClose: function () {