Skip to content

Commit

Permalink
Merge pull request #423 from d3x0r/patch-17
Browse files Browse the repository at this point in the history
Something like this should fix #422
  • Loading branch information
amark authored Sep 20, 2017
2 parents b3ba1bd + 1b8c75d commit 5c0a80a
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions lib/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -110,4 +115,4 @@ Gun.on('opt', function mount(ctx){
open(peer, as);
}, 2 * 1000);
}
});
});

0 comments on commit 5c0a80a

Please sign in to comment.