mDNS responder now sometimes responds directly

This commit is contained in:
tigerbot 2017-05-30 12:35:29 -06:00
parent 69d7d9e4b8
commit 810d0a8e90
1 changed files with 8 additions and 1 deletions

View File

@ -104,6 +104,7 @@ function createResponse(name, packet, ttl) {
module.exports.start = function (deps, config) {
var socket = require('dgram').createSocket({ type: 'udp4', reuseAddr: true });
var dns = require('dns-suite');
var nextBroadcast = -1;
socket.on('message', function (message, rinfo) {
// console.log('Received %d bytes from %s:%d', message.length, rinfo.address, rinfo.port);
@ -130,7 +131,13 @@ module.exports.start = function (deps, config) {
randomId.get().then(function (name) {
var resp = createResponse(name, packet, config.mdns.ttl);
var now = Date.now();
if (now > nextBroadcast) {
socket.send(resp, config.mdns.port, config.mdns.broadcast);
nextBroadcast = now + config.mdns.ttl * 1000;
} else {
socket.send(resp, rinfo.port, rinfo.address);
}
});
});