fixed server-to-server for @d3x0r

This commit is contained in:
Mark Nadal 2016-07-07 12:54:19 -07:00
parent 8160ae0c4a
commit 590b52d92b
6 changed files with 47 additions and 22 deletions

6
gun.js
View File

@ -1317,7 +1317,6 @@
o.url = opt.url;
cb = cb || function(){};
if(!o.base){ return }
if(opt.WebSocket){ o.WebSocket = opt.WebSocket }
r.transport(o, cb);
}
r.createServer = function(fn){ r.createServer.s.push(fn) }
@ -1333,7 +1332,7 @@
r.jsonp(opt, cb);
}
r.ws = function(opt, cb){
var ws, WS = opt.WebSocket || window.WebSocket || window.mozWebSocket || window.webkitWebSocket;
var ws, WS = r.WebSocket || window.WebSocket || window.mozWebSocket || window.webkitWebSocket;
if(!WS){ return }
if(ws = r.ws.peers[opt.base]){
if(!ws.readyState){ return setTimeout(function(){ r.ws(opt, cb) },10), true }
@ -1375,10 +1374,9 @@
if(!res){ return }
res.headers = res.headers || {};
if(res.headers['ws-rid']){ return (r.ws.cbs[res.headers['ws-rid']]||function(){})(null, res) }
//Gun.log("We have a pushed message!", res);
if(res.body){ r.createServer.ing(res, function(res){ r(opt.base, null, null, res)}) } // emit extra events.
};
ws.onerror = function(e){ Gun.log(e); };
ws.onerror = function(e){ console.log(e); };
return true;
}
r.ws.peers = {};

View File

@ -10,10 +10,10 @@
server = gun.__.opt.ws.server = gun.__.opt.ws.server || opt.ws.server || server;
require('./ws')(gun.wsp.ws = gun.wsp.ws || new ws(gun.__.opt.ws), function(req, res){
var ws = this;
req.headers['gun-sid'] = ws.sid = ws.sid? ws.sid : req.headers['gun-sid'];
req.headers['gun-sid'] = ws.sid = (ws.sid? ws.sid : req.headers['gun-sid']);
ws.sub = ws.sub || gun.wsp.on('network').event(function(msg){
if(!ws || !ws.send || !ws._socket || !ws._socket.writable){ return this.off() }
if(!msg || (msg.headers && msg.headers['gun-sid'] === ws.sid)){ return }
if(!msg || (ws.sid && msg.headers && msg.headers['gun-sid'] === ws.sid)){ return }
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));
@ -150,7 +150,7 @@
//Gun.log("GET!", req);
key = req.body;
//Gun.log("tran.get", key);
var opt = {key: false};
var opt = {key: false, local: true};
//gun.get(key, function(err, node){
(gun.__.opt.wire.get||function(key, cb){cb(null,null)})(key, function(err, node){
//Gun.log("tran.get", key, "<---", err, node);
@ -203,7 +203,7 @@
if(err){ return cb({headers: reply.headers, body: {err: err || "Failed."}}) } // TODO: err should already be an error object?
cb({headers: reply.headers, body: {ok: ok || "Persisted."}});
//Gun.log("tran.put <------------------------", ok);
});
}, {local: true});
}).err){ cb({headers: reply.headers, body: {err: req.err || "Union failed."}}) }
} else {
cb({headers: reply.headers, body: {err: "Not a valid graph!"}});
@ -227,17 +227,31 @@
var driver = {
put: function(graph, cb, opt){
put(graph, cb, opt);
var msg = {
headers: {'Content-Type': 'application/json', id: gun.wsp.msg()},
opt = opt || {};
if(opt.local){ return }
var id = gun.wsp.msg();
gun.wsp.on('network').emit({ // sent to dynamic peers!
headers: {'Content-Type': 'application/json', id: id},
body: graph
};
gun.wsp.on('network').emit(msg);
});
var ropt = {headers:{}, WebSocket: WebSocket};
ropt.headers.id = id;
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){
Gun.request(url, graph, function(err, reply){
reply.body = reply.body || reply.chunk || reply.end || reply.write;
if(err || !reply || (err = reply.body && reply.body.err)){
return cb({err: Gun.log(err || "Put failed.") });
}
cb(null, reply.body);
}, ropt);
});
},
get: function(lex, cb, opt){
get(lex, cb, opt);
if(!Gun.request){ return console.log("Server could not find default network abstraction.") }
opt = opt || {};
var ropt = {headers:{}, WebSocket: WebSocket};
if(opt.local){ return }
if(!Gun.request){ return console.log("Server could not find default network abstraction.") }
var ropt = {headers:{}};
ropt.headers.id = gun.wsp.msg();
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){
Gun.request(url, lex, function(err, reply){
@ -251,6 +265,8 @@
}
}
var WebSocket = require('ws');
Gun.request.WebSocket = WebSocket;
Gun.request.createServer(gun.wsp.wire);
gun.__.opt.wire = driver;
gun.opt({wire: driver}, true);
});

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.3.96",
"version": "0.3.97",
"description": "Graph engine",
"main": "index.js",
"scripts": {

View File

@ -1,14 +1,15 @@
//client.js writes data up to a listening hub.js, which relays to a server.js that reads the data.
var http = require('http');
var Gun = require('../../index');
var gun = Gun({
file: 'data.json'
file: 'http.json'
});
gun.get('data').put({a: {data: 1}, b: {data: 2}});
var server = http.createServer(function(req, res){});
gun.wsp(server);
server.listen(8081);
server.listen(8080);
console.log('Server started on port ' + 8081 + ' with /gun');
console.log('Server started on port ' + 8080 + ' with /gun');

View File

@ -1,6 +1,9 @@
var Gun = require('../../index');
var location = {host:"localhost"};
var gun = Gun( 'ws://' + location.host + ':8081/gun');
var gdb = gun.get( 'data' );
gdb.map(function(val,field){ console.log( field, "=", val ); } );
var gun = Gun( { file: 'read.json', peers: ['http://' + location.host + ':8080/gun'] });
gun.get( 'data' ).path('stuff').map(function(val,field){ console.log( field, "=", val ); } );
console.log( "done... wait forever?" );

View File

@ -0,0 +1,7 @@
var Gun = require('../../index');
var location = {host:"localhost"};
var gun = Gun( { file: 'write.json', peers: ['http://' + location.host + ':8080/gun'] });
gun.get( 'data' ).path('stuff').put({a: {data: 1}, b: {data: 2}});