diff --git a/lib/multicast.js b/lib/multicast.js index d2754ac9..4f1c7773 100644 --- a/lib/multicast.js +++ b/lib/multicast.js @@ -1,33 +1,29 @@ -var Gun = (typeof window !== 'undefined')? window.Gun : require('../gun'); - -var MULTICAST_ADDR = '233.255.255.255'; -var MULTICAST_INTERVAL = 1000; -var PORT = 20000; -var DEFAULT_GUN_PORT = 8765; -var ENC = 'utf8'; +var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); Gun.on('create', function(root){ this.to.next(root); - var opt = root.opt, u; + var opt = root.opt; + if(typeof window !== "undefined"){ + return; // do nothing for now - Chrome extensions could use multicast though + } if(false === opt.multicast){ return } opt.multicast = opt.multicast || {}; - if(typeof window === 'undefined'){ - nodeMulticast(root, opt); - } else if (chrome && chrome.sockets && chrome.sockets.udp) { - chromeMulticast(root, opt); - } -}); -function nodeMulticast(root, opt) { - var dgram = require('dgram'); - var process = require('process'); + var MULTICAST_ADDR = "233.255.255.255"; + var MULTICAST_INTERVAL = 1000; + var PORT = 20000; + var DEFAULT_GUN_PORT = 8765; + var ENC = 'utf8'; - socket = dgram.createSocket({ type: 'udp4', reuseAddr: true }); + var dgram = require("dgram"); + var process = require("process"); + + socket = dgram.createSocket({ type: "udp4", reuseAddr: true }); socket.bind(PORT); var address; - socket.on('listening', function() { + socket.on("listening", function() { socket.addMembership(MULTICAST_ADDR); if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node console.log(`Advertising this node (port ${opt.multicast.port}) on multicast (${MULTICAST_ADDR})`); @@ -45,11 +41,11 @@ function nodeMulticast(root, opt) { }; var message = Buffer.from(JSON.stringify(msgObj), ENC); socket.send(message, 0, message.length, PORT, MULTICAST_ADDR, function() { - // console.info(`Sending message '${message}'`); + // console.info(`Sending message "${message}"`); }); } - socket.on('message', function(message, rinfo) { + socket.on("message", function(message, rinfo) { try { var msgObj = JSON.parse(message.toString(ENC)); if (!(msgObj.gun && msgObj.gun.port)) { return } @@ -62,40 +58,4 @@ function nodeMulticast(root, opt) { // console.error(`Received multicast from ${rinfo.address}:${rinfo.port} but failed to connect:`, e); } }); -} - -function chromeMulticast(root, opt) { - chrome.sockets.udp.create({bufferSize: 1024 * 1024}, function (createInfo) { - var socketId = createInfo.socketId; - var ttl = 12; - chrome.sockets.udp.setMulticastTimeToLive(socketId, ttl, function (result) { - if (result != 0) { - console.error('Set TTL Error: ', 'Unknown error'); - } - chrome.sockets.udp.bind(socketId, '0.0.0.0', PORT, function (result) { - if (result != 0) { - chrome.sockets.udp.close(socketId, function () { - console.error('Error on bind(): ', result); - }); - } else { - chrome.sockets.udp.joinGroup(socketId, MULTICAST_ADDR, function (result) { - if (result != 0) { - chrome.sockets.udp.close(socketId, function () { - console.error('Error on joinGroup(): ', result); - }); - } else { - me.socketId = socketId; - chrome.sockets.udp.onReceive.addListener(info => { - console.log('msg received', info); - }); - chrome.sockets.udp.onReceiveError.addListener(info => { - console.error('receive error'); - }); - console.log('Chrome multicast socket listening'); - } - }); - } - }); - }); - }); -} +}); diff --git a/package.json b/package.json index 0222ac12..c6601276 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.9.101", + "version": "0.9.102", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "main": "index.js", "browser": "gun.min.js",