forked from guerrerocarlos/chromecast-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.39 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
50
var util = require( 'util' );
var events = require( 'events' );
var ssdp = require('node-ssdp').Client;
var http = require('http');
var Device = require('./device').Device;
var debug = require('debug')('chromecast-js');
exports.Device = Device;
var Browser = function( options ) {
events.EventEmitter.call( this );
this.init( options );
};
util.inherits(Browser, events.EventEmitter );
exports.Browser = Browser;
Browser.prototype.update = function( device ) {
var dev_config = {addresses: device.addresses, name: device.name};
this.device = new Device(dev_config);
this.emit('deviceOn', this.device);
}
Browser.prototype.init = function( options ) {
var self = this;
var ssdpBrowser = new ssdp();
ssdpBrowser.on('response', function (headers, statusCode, rinfo) {
if (statusCode != 200)
return;
if (!headers['LOCATION'])
return;
var request = http.get(headers['LOCATION'], function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
if (body.search('<SCPDURL>http://www.google.com/cast</SCPDURL>') == -1)
return;
var match = body.match(/<friendlyName>(.+?)<\/friendlyName>/);
if (!match || match.length != 2)
return;
self.update({addresses: [rinfo.address], name: match[1]});
});
});
});
ssdpBrowser.search('urn:dial-multiscreen-org:service:dial:1');
};