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