From c7e3a80f425e3460ea612ad31a21e655d16bdbb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Sun, 14 Feb 2016 22:15:38 +0800 Subject: [PATCH] Added re-auth logic --- notify-term/Notis.js | 1 + notify-term/WNSAuth.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/notify-term/Notis.js b/notify-term/Notis.js index 1e1d497..a546c0a 100644 --- a/notify-term/Notis.js +++ b/notify-term/Notis.js @@ -12,6 +12,7 @@ class Notis { this.__valid = false; this.__error = null; + this.Retry = 0; try { diff --git a/notify-term/WNSAuth.js b/notify-term/WNSAuth.js index b44a729..f7c70df 100644 --- a/notify-term/WNSAuth.js +++ b/notify-term/WNSAuth.js @@ -115,6 +115,7 @@ class WNSAuth extends EventEmitter Deliver( NotisQ ) { + var _self = this; Model.Tokens .findOne({ name: NotisQ.id }) .exec( ( err, data ) => { @@ -128,6 +129,26 @@ class WNSAuth extends EventEmitter { this.__send( data.token, NotisQ, ( sender, e ) => { Dragonfly.Debug( "Send: " + e.statusCode ); + + if( e.statusCode != 200 ) + { + AuthToken = null; + Dragonfly.Info( "Perhaps access token is expired, retrying ..." ); + + if( NotisQ.Retry < 2 ) + { + _self.once( "AuthComplete", () => { + NotisQ.Retry ++; + _self.Deliver( NotisQ ); + }); + } + else + { + Dragonfly.Info( "Retrying exceeded the limit, dropping the message" ); + } + + _self.Authenticate(); + } } ); } else