From 6795ef04a88ae553febc39bdf06ccaa6089af2eb Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 23 Jan 2019 14:28:33 +0200 Subject: [PATCH] better env detection --- lib/multicast.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/multicast.js b/lib/multicast.js index 4f1c7773..98c4f393 100644 --- a/lib/multicast.js +++ b/lib/multicast.js @@ -3,9 +3,15 @@ var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); Gun.on('create', function(root){ this.to.next(root); var opt = root.opt; - if(typeof window !== "undefined"){ - return; // do nothing for now - Chrome extensions could use multicast though + + try { + var dgram = require("dgram"); + var process = require("process"); + } catch (e) { + console.log('multicast is not available'); + return; } + if(false === opt.multicast){ return } opt.multicast = opt.multicast || {}; @@ -15,9 +21,6 @@ Gun.on('create', function(root){ var DEFAULT_GUN_PORT = 8765; var ENC = 'utf8'; - var dgram = require("dgram"); - var process = require("process"); - socket = dgram.createSocket({ type: "udp4", reuseAddr: true }); socket.bind(PORT);