From c18d4d3e2bf90222e493785db21758c51c344bd0 Mon Sep 17 00:00:00 2001 From: Timon Reinhard Date: Sun, 23 Oct 2016 18:49:59 +0200 Subject: [PATCH] Recover from failed subscription requests (fixes #42) --- client.js | 15 ++++++++++----- test/index.js | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/client.js b/client.js index ba7eda9..d7e5706 100644 --- a/client.js +++ b/client.js @@ -283,8 +283,7 @@ WemoClient.prototype._subscribe = function(serviceType) { throw new Error('Service ' + serviceType + ' not supported by ' + this.UDN); } if (!this.callbackURL) { - debug('no callback URL - returning'); - return; + throw new Error('Can not subscribe without callbackURL'); } if (this.subscriptions[serviceType] && this.subscriptions[serviceType] === 'PENDING') { debug('subscription still pending'); @@ -297,7 +296,7 @@ WemoClient.prototype._subscribe = function(serviceType) { path: this.services[serviceType].eventSubURL, method: 'SUBSCRIBE', headers: { - TIMEOUT: 'Second-130' + TIMEOUT: 'Second-300' } }; @@ -314,9 +313,15 @@ WemoClient.prototype._subscribe = function(serviceType) { } var req = http.request(options, function(res) { - if (res.headers.sid) { + if (res.statusCode === 200) { + // Renew after 150 seconds this.subscriptions[serviceType] = res.headers.sid; - setTimeout(this._subscribe.bind(this), 120 * 1000, serviceType); + setTimeout(this._subscribe.bind(this), 150 * 1000, serviceType); + } else { + // Try to recover from failed subscription after 2 seconds + debug('Subscription request failed with HTTP %s', res.statusCode); + this.subscriptions[serviceType] = null; + setTimeout(this._subscribe.bind(this), 2000, serviceType); } }.bind(this)); diff --git a/test/index.js b/test/index.js index 52d4aa8..c2f6720 100644 --- a/test/index.js +++ b/test/index.js @@ -20,6 +20,7 @@ describe('Wemo', function() { var wemo = new Wemo(); var client = wemo.client(deviceInfo); var address = url.parse(wemo.getCallbackURL()); + client.callbackURL = address client.on('binaryState', function(state) { state.must.be('1'); @@ -67,6 +68,7 @@ describe('WemoClient', function() { beforeEach(function() { mitm = Mitm(); client = (new Wemo()).client(deviceInfo); + client.callbackURL = 'http://localhost' }); afterEach(function() {