-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
mdns-tests.ts
84 lines (67 loc) · 2.17 KB
/
mdns-tests.ts
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
77
78
79
80
81
82
83
84
/**
* Created by stefansteinhart on 30.01.15.
*/
/// <reference path="mdns.d.ts" />
var mdns = require('mdns')
var ad:MDNS.Advertisement = mdns.createAdvertisement(mdns.tcp('http'), 4321);
ad.start();
var browser = mdns.createBrowser(mdns.tcp('http'));
browser.on('serviceUp', function(service:MDNS.Service) {
console.log("service up: ", service);
});
browser.on('serviceDown', function(service:MDNS.Service) {
console.log("service down: ", service);
});
browser.start();
var r0 = mdns.tcp('http') // string form: _http._tcp
, r1 = mdns.udp('osc', 'api-v1') // string form: _osc._udp,_api-v1
, r2 = new mdns.ServiceType('http', 'tcp') // string form: _http._tcp
, r3 = mdns.makeServiceType('https', 'tcp') // string form: _https._tcp
;
var txt_record = {
name: 'bacon'
, chunky: true
, strips: 5
};
var ad:MDNS.Advertisement = mdns.createAdvertisement(mdns.tcp('http'), 4321, {txtRecord: txt_record});
var sequence = [
mdns.rst.DNSServiceResolve()
, mdns.rst.DNSServiceGetAddrInfo({families: [4] })
];
var browser = mdns.createBrowser(mdns.tcp('http'), {resolverSequence: sequence});
interface HammerTimeService extends MDNS.Service {
hammerTime:Date;
}
function MCHammer(options:any) {
options = options || {};
return function MCHammer(service:HammerTimeService, next:()=>void) {
console.log('STOP!');
setTimeout(function() {
console.log('hammertime...');
service.hammerTime = new Date();
next();
}, options.delay || 1000);
}
}
var browser = mdns.createBrowser( mdns.tcp('http')
, { networkInterface: mdns.loopbackInterface()});
var ad:MDNS.Advertisement;
function createAdvertisement() {
try {
ad = mdns.createAdvertisement(mdns.tcp('http'), 1337);
ad.on('error', handleError);
ad.start();
} catch (ex) {
handleError(ex);
}
}
function handleError(error:MDNS.DnsSdError) {
switch (error.errorCode) {
case mdns.kDNSServiceErr_Unknown:
console.warn(error);
setTimeout(createAdvertisement, 5000);
break;
default:
throw error;
}
}