added handling for 'hello' from the server

This commit is contained in:
tigerbot 2017-04-28 15:02:44 -06:00
parent 876fa47e02
commit 12faab1acf
1 changed files with 28 additions and 14 deletions

View File

@ -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 = { var packerHandlers = {
oncontrol: function (opts) { oncontrol: function (opts) {
var cmd, err; var cmd, err;
@ -119,8 +131,22 @@ function run(copts) {
return; return;
} }
// TODO: handle a "hello" message that let's us know we're authenticated. if (cmd[0] === 0) {
err = { message: 'unknown command '+cmd[1], code: 'E_UNKNOWN_COMMAND' }; 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')); wsHandlers.sendMessage(Packer.pack(null, [-cmd[0], err], 'control'));
} }
@ -134,8 +160,6 @@ function run(copts) {
var str; var str;
var m; var m;
authenticated = true;
if (localclients[cid]) { if (localclients[cid]) {
//console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength); //console.log("[=>] received data from '" + cid + "' =>", opts.data.byteLength);
localclients[cid].write(opts.data); localclients[cid].write(opts.data);
@ -268,16 +292,6 @@ function run(copts) {
console.info("[open] connected to '" + copts.stunneld + "'"); console.info("[open] connected to '" + copts.stunneld + "'");
wsHandlers.refreshTimeout(); wsHandlers.refreshTimeout();
timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout); 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 () { , onClose: function () {