From 7573b86946aedd5f4d86bc6b5280f1e61850ffb9 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 4 May 2017 11:36:24 +0200 Subject: [PATCH] call back with error if an RPC is made while disconnected --- www/common/rpc.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/www/common/rpc.js b/www/common/rpc.js index b69a5ce5a..2e1114729 100644 --- a/www/common/rpc.js +++ b/www/common/rpc.js @@ -102,9 +102,16 @@ types of messages: timeouts: {}, // timeouts pending: {}, // callbacks cookie: null, + connected: true, }; var send = function (type, msg, cb) { + if (!ctx.connected && type !== 'COOKIE') { + return void window.setTimeout(function () { + cb('DISCONNECTED'); + }); + } + // construct a signed message... var data = [type, msg]; @@ -127,6 +134,17 @@ types of messages: onMsg(ctx, msg); }); + network.on('disconnect', function (reason) { + ctx.connected = false; + }); + + network.on('reconnect', function (uid) { + send('COOKIE', "", function (e, msg) { + if (e) { return void cb(e); } + ctx.connected = true; + }); + }); + send('COOKIE', "", function (e, msg) { if (e) { return void cb(e); } // callback to provide 'send' method to whatever needs it