Skip to content

Commit

Permalink
Add ability to stop broadcast
Browse files Browse the repository at this point in the history
Sometimes you just wanna send data to only one client, not the whole
bunch. Now, with new advances in gun technology, you can disable the
default broadcasting behavior.
Feature implemented under the direction and guidance of @amark.
  • Loading branch information
PsychoLlama committed Oct 14, 2016
1 parent f146f0a commit 455cec4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/wsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
if(msg && msg.headers){ delete msg.headers['ws-rid'] }
// TODO: BUG? ^ What if other peers want to ack? Do they use the ws-rid or a gun declared id?
try{ws.send(Gun.text.ify(msg));
}catch(e){} // juuuust in case.
}catch(e){} // juuuust in case.
});
gun.wsp.wire(req, res);
});
Expand Down Expand Up @@ -104,7 +104,7 @@
Gun.obj.del(gun.wsp.msg.debounce, id);
});
},500);
if(id = gun.wsp.msg.debounce[id]){
if(id = gun.wsp.msg.debounce[id]){
return gun.wsp.msg.debounce[id] = Gun.time.is(), id;
}
};
Expand All @@ -113,9 +113,11 @@
// all streams, technically PATCH but implemented as PUT or POST, are forwarded to other trusted peers
// except for the ones that are listed in the message as having already been sending to.
// all states, implemented with GET, are replied to the source that asked for it.
function flow(req, res){
gun.wsp.on('network').emit(Gun.obj.copy(req));
if(req.headers.rid){ return } // no need to process.
function flow(req, res){
if (!req.auth || req.headers.broadcast) {
gun.wsp.on('network').emit(Gun.obj.copy(req));
}
if(req.headers.rid){ return } // no need to process.
if(Gun.is.lex(req.body)){ return tran.get(req, res) }
else { return tran.put(req, res) }
}
Expand Down Expand Up @@ -159,7 +161,7 @@
if(opt.on && opt.on.off){ opt.on.off() }
return cb({headers: reply.headers, body: (err? (err.err? err : {err: err || "Unknown error."}) : null)});
}
if(Gun.obj.empty(node)){
if(Gun.obj.empty(node)){
if(opt.on && opt.on.off){ opt.on.off() }
return cb({headers: reply.headers, body: node});
} // we're out of stuff!
Expand Down Expand Up @@ -191,7 +193,7 @@
// This will give you much more fine-grain control over security, transactions, and what not.
var reply = {headers: {'Content-Type': tran.json, rid: req.headers.id, id: gun.wsp.msg()}};
if(!req.body){ return cb({headers: reply.headers, body: {err: "No body"}}) }
//Gun.log("\n\ntran.put ----------------->", req.body);
//Gun.log("\n\ntran.put ----------------->", req.body);
if(Gun.is.graph(req.body)){
if(req.err = Gun.union(gun, req.body, function(err, ctx){ // TODO: BUG? Probably should give me ctx.graph
if(err){ return cb({headers: reply.headers, body: {err: err || "Union failed."}}) }
Expand Down Expand Up @@ -270,4 +272,4 @@
gun.__.opt.wire = driver;
gun.opt({wire: driver}, true);
});
}({}));
}({}));

0 comments on commit 455cec4

Please sign in to comment.