forked from mhdawson/dlna-browser-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.js
76 lines (67 loc) · 2.24 KB
/
example2.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
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
const url = require('url');
const http = require('http');
const xmltojs = require('xml2js');
const browseServer = require('./lib/index.js');
const nodessdp = require('node-ssdp').Client;
const node_ssdp_client = new nodessdp();
const mediaServerName = process.argv[2];
var done = false;
node_ssdp_client.on('response', function (headers, statusCode, rinfo) {
const requestUrl = url.parse(headers.LOCATION);
const httpOptions = {
host: requestUrl.hostname,
port: requestUrl.port,
path: requestUrl.pathname
}
const req = http.request(httpOptions, function(response) {
var data = ''
response.on('data', function(newData) {
data = data + newData;
});
response.on('end', function() {
if (done == true) {
return;
}
xmltojs.parseString(data, function(err, result) {
if(result.root.device[0].friendlyName.toString() === mediaServerName) {
done = true;
if (result.root.device[0].serviceList[0].service[0].serviceType[0] ===
'urn:schemas-upnp-org:service:ContentDirectory:1') {
const controlUrl =
'http://' +
requestUrl.hostname +
':' +
requestUrl.port +
result.root.device[0].serviceList[0].service[0].controlURL[0];
browseServer('0', controlUrl, {}, function(err, result) {
if (err) {
console.log(err);
return;
}
if (result.container) {
for (let i = 0; i < result.container.length; i++) {
console.log('Container:' + result.container[i].id);
}
}
if (result.item) {
for (let i = 0; i < result.item.length; i++) {
console.log('Item:' + result.item[i].title);
}
}
});
};
};
});
});
});
req.on('error', function(err) {
console.log(err);
});
req.end();
});
// search for media server and display top level content
node_ssdp_client.search('urn:schemas-upnp-org:device:MediaServer:1');
setTimeout(function() {
console.log('done');
}, 10000);