From db8a352afe5fc9f098c83a06d5dad0bc65465693 Mon Sep 17 00:00:00 2001 From: Moritz Horstmann Date: Tue, 12 Dec 2017 22:12:23 +0100 Subject: [PATCH] Fix race condition in ros websocket authentication Due to asynchronous authentication, the episode may be set before an auth token is sent to rosbridge, leading to an early json prolog query for mongo db selection and connection termination. --- knowrob/client.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/knowrob/client.js b/knowrob/client.js index 3eb4d53..2cbed04 100644 --- a/knowrob/client.js +++ b/knowrob/client.js @@ -88,9 +88,7 @@ function KnowrobClient(options){ that.connect(); that.episode = new KnowrobEpisode(that); - if(options.category && options.episode) - that.episode.setEpisode(options.category, options.episode); - + that.createOverlay(); if(requireEpisode && !that.episode.hasEpisode()) { @@ -113,6 +111,12 @@ function KnowrobClient(options){ }); }; + function onRosConnected() { + that.registerNodes(); + if(options.category && options.episode) + that.episode.setEpisode(options.category, options.episode); + } + this.connect = function () { if(that.ros) return; that.ros = new ROSLIB.Ros({url : rosURL}); @@ -121,11 +125,11 @@ function KnowrobClient(options){ console.log('Connected to websocket server.'); if (authentication) { // Acquire auth token for current user and authenticate, then call registerNodes - that.authenticate(authURL, that.registerNodes); + that.authenticate(authURL, onRosConnected); } else { // No authentication requested, call registerNodes directly - that.registerNodes(); that.waitForJsonProlog(); + onRosConnected(); } }); that.ros.on('close', function() {