Skip to content

Commit

Permalink
Recover from failed subscription requests (fixes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
timonreinhard committed Oct 23, 2016
1 parent af36f7a commit c18d4d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -297,7 +296,7 @@ WemoClient.prototype._subscribe = function(serviceType) {
path: this.services[serviceType].eventSubURL,
method: 'SUBSCRIBE',
headers: {
TIMEOUT: 'Second-130'
TIMEOUT: 'Second-300'
}
};

Expand All @@ -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));

Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -67,6 +68,7 @@ describe('WemoClient', function() {
beforeEach(function() {
mitm = Mitm();
client = (new Wemo()).client(deviceInfo);
client.callbackURL = 'http://localhost'
});

afterEach(function() {
Expand Down

0 comments on commit c18d4d3

Please sign in to comment.