-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (47 loc) · 1.22 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const util = require('util');
const { TimedDiscovery, search, addService } = require('tinkerhub-discovery');
const Client = require('node-ssdp').Client;
class SSDP extends TimedDiscovery {
static get type() {
return 'ssdp';
}
constructor(searchType, cacheTime) {
super({ maxStaleTime: (cacheTime || 1800) * 1000 });
this.searchType = searchType;
this._client = new Client();
this._client.on('response', this._handleResponse.bind(this));
this._client.on('advertise-alive', this._handleAlive.bind(this));
this._client.on('advertise-bye', this._handleBye.bind(this));
}
start() {
super.start();
this._client.start();
}
stop() {
super.stop();
this._client.stop();
}
[search]() {
this._client.search(this.searchType);
}
_handleResponse(headers, statusCode) {
if(statusCode != 200) return;
const service = {
id: headers.USN,
location: headers.LOCATION,
headers: headers
};
this[addService](service);
}
_handleAlive() {
}
_handleBye() {
}
[util.inspect.custom](depth, options) {
return options.stylize('SSDP', 'special') + '{type=' + this.searchType + '}';
}
}
module.exports.browser = function(searchType, cacheTime) {
return new SSDP(searchType, cacheTime);
};