Something like this should fix #422

Added support to take opt as a string
Promote simple opt as a string to opt.peers[opt] .
This commit is contained in:
Jim B 2017-09-19 03:59:26 -07:00 committed by d3x0r
parent b3ba1bde4d
commit 1b8c75d786

View File

@ -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);
}
});
});