node-upnp-subscription
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -59,3 +59,3 @@ var Subscription = require('../index.js'); | ||
var host = '192.168.1.164'; | ||
var host = '192.168.1.157'; | ||
var port = 55178; | ||
@@ -62,0 +62,0 @@ var infoSubUri = '/Ds/Info/event'; |
39
index.js
@@ -10,6 +10,25 @@ "use strict"; | ||
let httpServerPort; | ||
let httpSubscriptionResponseServer; | ||
let subscriptions = new Map(); | ||
portfinder.getPort(function (err, availablePort) { | ||
httpSubscriptionResponseServer = http.createServer(); | ||
httpServerPort = availablePort; | ||
httpSubscriptionResponseServer.listen(httpServerPort, () => { | ||
httpSubscriptionResponseServer.on('request', (req) => { | ||
let sid = req.headers.sid; | ||
let handle = xmlResponseParser((err, data) => { | ||
let emitter = subscriptions.get(sid); | ||
emitter.emit('message', { sid: sid, body: data }); | ||
}); | ||
handle(req); | ||
}); | ||
}); | ||
}); | ||
function Subscription(host, port, eventSub, requestedTimeoutSeconds) { | ||
let sid, | ||
resubscribeTimeout, | ||
httpSubscriptionResponseServer, | ||
emitter = this, | ||
@@ -37,3 +56,3 @@ timeoutSeconds = requestedTimeoutSeconds || 1800; | ||
} | ||
function subscribe(availablePort) { | ||
function subscribe() { | ||
http.request({ | ||
@@ -45,3 +64,3 @@ host: host, | ||
headers: { | ||
'CALLBACK': "<http://" + ip.address() + ':' + availablePort + ">", | ||
'CALLBACK': "<http://" + ip.address() + ':' + httpServerPort + ">", | ||
'NT': 'upnp:event', | ||
@@ -59,5 +78,7 @@ 'TIMEOUT': 'Second-' + timeoutSeconds | ||
} | ||
resubscribeTimeout = setTimeout(resubscribe, (timeoutSeconds-1) * 1000) | ||
resubscribeTimeout = setTimeout(resubscribe, (timeoutSeconds-1) * 1000); | ||
subscriptions.set(sid, emitter); | ||
}).on('error', function(e) { | ||
emitter.emit('error', e); | ||
subscriptions.delete(sid); | ||
}).end(); | ||
@@ -67,3 +88,2 @@ } | ||
clearTimeout(resubscribeTimeout); | ||
httpSubscriptionResponseServer.close(); | ||
if (sid) { | ||
@@ -86,12 +106,7 @@ http.request({ | ||
} | ||
subscriptions.delete(sid); | ||
}; | ||
portfinder.getPort(function (err, availablePort) { | ||
httpSubscriptionResponseServer = http.createServer(); | ||
httpSubscriptionResponseServer.on('request', xmlResponseParser((err, data) => { | ||
emitter.emit('message', { sid: sid, body: data }); | ||
})); | ||
httpSubscriptionResponseServer.listen(availablePort, () => subscribe(availablePort)); | ||
}); | ||
subscribe(); | ||
} | ||
util.inherits(Subscription, events.EventEmitter); | ||
module.exports = Subscription; |
@@ -9,3 +9,3 @@ { | ||
"description": "Upnp subscription library for Node, handling subscription, renewal and expiry.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "index.js", | ||
@@ -12,0 +12,0 @@ "directories": {}, |
@@ -30,4 +30,2 @@ "use strict"; | ||
subscription.unsubscribe(); | ||
}); | ||
after(function() { | ||
nock.cleanAll(); | ||
@@ -34,0 +32,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22086
420
2