forked from RobCoIndustries/pipboylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.14 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
var dgram = require('dgram')
var FALLOUT_UDP_PORT = require('./lib/constants').FALLOUT_UDP_PORT
var FALLOUT_TCP_PORT = require('./lib/constants').FALLOUT_TCP_PORT
var AUTODISCOVERY_PAYLOAD = '{"cmd":"autodiscover"}'
var DiscoveryClient = function DiscoveryClient () {
this.client = dgram.createSocket('udp4')
}
DiscoveryClient.prototype.discover = function discover (cb) {
var autodiscover = function autodiscover () {
this.setBroadcast(true)
var message = new Buffer(AUTODISCOVERY_PAYLOAD)
this.send(message, 0, message.length, FALLOUT_UDP_PORT, '255.255.255.255', function (err) {
if (err) {
cb(err)
}
})
this.on('message', function (msg, rinfo) {
try {
var server = JSON.parse(msg.toString())
server.info = rinfo
cb(undefined, server)
} catch (e) {
cb(e, undefined)
return
}
})
}
this.client.bind(undefined, undefined, autodiscover)
}
DiscoveryClient.prototype.close = function close (cb) {
this.client.close(cb)
}
module.exports = {
DiscoveryClient: DiscoveryClient,
FALLOUT_UDP_PORT: FALLOUT_UDP_PORT,
FALLOUT_TCP_PORT: FALLOUT_TCP_PORT
}