-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
49 lines (41 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
var events = require('events');
var http = require('http');
var util = require('util');
var Device = require('./device').Device;
var SsdpClient = require('node-ssdp').Client;
function Browser(options) {
events.EventEmitter.call(this);
this.init(options);
}
Browser.prototype.update = function(device) {
var devConfig = {addresses: device.addresses, name: device.name};
this.device = new Device(devConfig);
this.emit('deviceOn', this.device);
};
Browser.prototype.init = function() {
var _this = this;
var ssdpBrowser = new SsdpClient();
ssdpBrowser.on('response', function(headers, statusCode, rinfo) {
if (statusCode !== 200 || !headers.LOCATION) {
return;
}
http.get(headers.LOCATION, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var match = body.match(/<friendlyName>(.+?)<\/friendlyName>/);
if (!match || match.length !== 2) {
return;
}
_this.update({addresses: [rinfo.address], name: match[1]});
});
});
});
ssdpBrowser.search('urn:dial-multiscreen-org:service:dial:1');
};
util.inherits(Browser, events.EventEmitter);
exports.Browser = Browser;
exports.Device = Device;