From 1b8c75d786b28d54986809de6b8cc3eace638855 Mon Sep 17 00:00:00 2001 From: Jim B Date: Tue, 19 Sep 2017 03:59:26 -0700 Subject: [PATCH] Something like this should fix #422 Added support to take opt as a string Promote simple opt as a string to opt.peers[opt] . --- lib/ws.js | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/ws.js b/lib/ws.js index 0742b66c4..bfdab1c83 100644 --- a/lib/ws.js +++ b/lib/ws.js @@ -7,32 +7,37 @@ var url = require('url'); Gun.on('opt', function mount(ctx){ this.to.next(ctx); var opt = ctx.opt; + if( !opt.peers ) + if( typeof( opt == "string" ) ) + opt.peers = [opt]; + if(ctx.once){ return } - if(!opt.web){ return } var ws = opt.ws || (opt.ws = {}), batch; - ws.server = ws.server || opt.web; - ws.path = ws.path || '/gun'; + if(opt.web){ + ws.server = ws.server || opt.web; + ws.path = ws.path || '/gun'; - ws.web = new WebSocket.Server(ws); - - ws.web.on('connection', function(wire){ - wire.upgradeReq = wire.upgradeReq || {}; - wire.url = url.parse(wire.upgradeReq.url||'', true); - wire.id = wire.id || Gun.text.random(6); - var peer = opt.peers[wire.id] = {wire: wire}; - wire.peer = function(){ return peer }; - ctx.on('hi', peer); - wire.on('message', function(msg){ - //console.log("MESSAGE", msg); - receive(msg, wire, ctx); // diff: wire is wire. - }); - wire.on('close', function(){ - ctx.on('bye', peer); - Gun.obj.del(opt.peers, wire.id); - }); - }); + ws.web = new WebSocket.Server(ws); + ws.web.on('connection', function(wire){ + wire.upgradeReq = wire.upgradeReq || {}; + wire.url = url.parse(wire.upgradeReq.url||'', true); + wire.id = wire.id || Gun.text.random(6); + var peer = opt.peers[wire.id] = {wire: wire}; + wire.peer = function(){ return peer }; + ctx.on('hi', peer); + wire.on('message', function(msg){ + //console.log("MESSAGE", msg); + receive(msg, wire, ctx); // diff: wire is wire. + }); + wire.on('close', function(){ + ctx.on('bye', peer); + Gun.obj.del(opt.peers, wire.id); + }); + }); + } + ctx.on('out', function(at){ this.to.next(at); batch = JSON.stringify(at); @@ -110,4 +115,4 @@ Gun.on('opt', function mount(ctx){ open(peer, as); }, 2 * 1000); } -}); \ No newline at end of file +});