set multicast advertised port in examples/http.js

This commit is contained in:
Martti Malmi 2019-01-08 14:09:56 +02:00
parent ad079ad49b
commit 058bd3cae9
3 changed files with 28 additions and 24 deletions

View File

@ -16,6 +16,7 @@
config.server = require('http').createServer(Gun.serve(__dirname)); config.server = require('http').createServer(Gun.serve(__dirname));
} }
var gun = Gun({web: config.server.listen(config.port) }); 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'); console.log('Relay peer started on port ' + config.port + ' with /gun');
}()); }());

View File

@ -10,7 +10,9 @@ Gun.on('create', function(root){
opt.multicast = opt.multicast || {}; opt.multicast = opt.multicast || {};
var MULTICAST_ADDR = "233.255.255.255"; var MULTICAST_ADDR = "233.255.255.255";
var MULTICAST_INTERVAL = 1000;
var PORT = 20000; var PORT = 20000;
var DEFAULT_GUN_PORT = 8765;
var ENC = 'utf8'; var ENC = 'utf8';
var dgram = require("dgram"); var dgram = require("dgram");
@ -24,7 +26,8 @@ Gun.on('create', function(root){
socket.on("listening", function() { socket.on("listening", function() {
socket.addMembership(MULTICAST_ADDR); socket.addMembership(MULTICAST_ADDR);
if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node 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(); address = socket.address();
}); });
@ -33,7 +36,7 @@ Gun.on('create', function(root){
var msgObj = { var msgObj = {
gun: { gun: {
version: Gun.version, version: Gun.version,
port: opt.multicast.port || 8765 port: opt.multicast.port || DEFAULT_GUN_PORT
} }
}; };
var message = Buffer.from(JSON.stringify(msgObj), ENC); var message = Buffer.from(JSON.stringify(msgObj), ENC);