From 1e714f8fac135616cdb0ae664b5d863cc54f2636 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Tue, 8 Jan 2019 14:09:56 +0200 Subject: [PATCH] set multicast advertised port in examples/http.js --- examples/http.js | 43 ++++++++++++++++++++++--------------------- lib/multicast.js | 7 +++++-- lib/server.js | 2 +- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/examples/http.js b/examples/http.js index ceacfd1b..ba7a3847 100644 --- a/examples/http.js +++ b/examples/http.js @@ -1,21 +1,22 @@ -;(function(){ - var cluster = require('cluster'); - if(cluster.isMaster){ - return cluster.fork() && cluster.on('exit', function(){ cluster.fork() }); - } - - var fs = require('fs'); - var config = { port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765 }; - var Gun = require('../'); // require('gun') - - if(process.env.HTTPS_KEY){ - config.key = fs.readFileSync(process.env.HTTPS_KEY); - config.cert = fs.readFileSync(process.env.HTTPS_CERT); - config.server = require('https').createServer(config, Gun.serve(__dirname)); - } else { - config.server = require('http').createServer(Gun.serve(__dirname)); - } - - var gun = Gun({web: config.server.listen(config.port) }); - console.log('Relay peer started on port ' + config.port + ' with /gun'); -}()); \ No newline at end of file +;(function(){ + var cluster = require('cluster'); + if(cluster.isMaster){ + return cluster.fork() && cluster.on('exit', function(){ cluster.fork() }); + } + + var fs = require('fs'); + var config = { port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765 }; + var Gun = require('../'); // require('gun') + + if(process.env.HTTPS_KEY){ + config.key = fs.readFileSync(process.env.HTTPS_KEY); + config.cert = fs.readFileSync(process.env.HTTPS_CERT); + config.server = require('https').createServer(config, Gun.serve(__dirname)); + } else { + config.server = require('http').createServer(Gun.serve(__dirname)); + } + + require('../lib/multicast'); + var gun = Gun({web: config.server.listen(config.port), multicast: { port: 8765 } }); + console.log('Relay peer started on port ' + config.port + ' with /gun'); +}()); diff --git a/lib/multicast.js b/lib/multicast.js index 83b781a1..4f1c7773 100644 --- a/lib/multicast.js +++ b/lib/multicast.js @@ -10,7 +10,9 @@ Gun.on('create', function(root){ opt.multicast = opt.multicast || {}; var MULTICAST_ADDR = "233.255.255.255"; + var MULTICAST_INTERVAL = 1000; var PORT = 20000; + var DEFAULT_GUN_PORT = 8765; var ENC = 'utf8'; var dgram = require("dgram"); @@ -24,7 +26,8 @@ Gun.on('create', function(root){ socket.on("listening", function() { socket.addMembership(MULTICAST_ADDR); if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node - setInterval(sendMessage, 1000); + console.log(`Advertising this node (port ${opt.multicast.port}) on multicast (${MULTICAST_ADDR})`); + setInterval(sendMessage, MULTICAST_INTERVAL); } address = socket.address(); }); @@ -33,7 +36,7 @@ Gun.on('create', function(root){ var msgObj = { gun: { version: Gun.version, - port: opt.multicast.port || 8765 + port: opt.multicast.port || DEFAULT_GUN_PORT } }; var message = Buffer.from(JSON.stringify(msgObj), ENC); diff --git a/lib/server.js b/lib/server.js index 7870006c..026369e1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -18,4 +18,4 @@ require('./evict'); if('debug' === process.env.GUN_ENV){ require('./debug') } module.exports = Gun; -}()); \ No newline at end of file +}());