Skip to content

Instantly share code, notes, and snippets.

@lacyrhoades
Created October 10, 2016 04:50
Show Gist options
  • Save lacyrhoades/893ac68b156c63a721707f88bdcd7d35 to your computer and use it in GitHub Desktop.
Save lacyrhoades/893ac68b156c63a721707f88bdcd7d35 to your computer and use it in GitHub Desktop.
diff --git a/lib/lighthouse.js b/lib/lighthouse.js
index f9a00c8..2a292e9 100644
--- a/lib/lighthouse.js
+++ b/lib/lighthouse.js
@@ -2,11 +2,13 @@ var Netmask = require('netmask').Netmask
var dgram = require('dgram');
var os = require('os');
+var PORT = 3001;
+var MULTICAST_ADDR = "224.1.1.1";
+var BROADCAST_ADDR = "0.0.0.0";
+
var lighthouse = function Lighthouse() {
this.timer = null;
this.interval = 1;
- this.port = 3001;
- this.multicast_address = "224.1.1.1";
this.server = dgram.createSocket({type: "udp4", reuseAddr: true});
}
@@ -31,7 +33,10 @@ lighthouse.prototype.listen = function() {
console.log(`server close:\n${err.stack}`);
});
- self.server.bind(self.port);
+ self.server.bind(PORT, BROADCAST_ADDR, function() {
+ self.server.addMembership(MULTICAST_ADDR);
+ self.server.setBroadcast(true);
+ });
}
lighthouse.prototype.startBroadcasting = function() {
@@ -60,9 +65,8 @@ lighthouse.prototype.startBroadcasting = function() {
self.message = new Buffer("{server: '" + self.address + "'}");
block = new Netmask(self.address + '/' + self.mask);
- self.braodcast_address = block.broadcast;
- console.log(`Broadcasting to: ${self.braodcast_address}:${self.port}`);
+ console.log(`Broadcasting to: ${block.broadcast}:${PORT}`);
console.log(String(self.message));
}
@@ -78,7 +82,7 @@ lighthouse.prototype.rebroadcast = function() {
var self = this;
if (self.message) {
- self.server.send(self.message, 0, self.message.length, self.port, self.broadcast_address, function() {
+ self.server.send(self.message, 0, self.message.length, PORT, MULTICAST_ADDR, function() {
console.log(`Sent message: ${self.message}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment