Created
November 18, 2016 23:36
-
-
Save prozacgod/38478fe7b6ee67369f98f6bbf0447839 to your computer and use it in GitHub Desktop.
dnode example - "Non Trivial"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is a somewhat non-trivial dnode example setup | |
// the idea here is to provide example of unified communication for client and server to be able to | |
// communicate against their api's within scope of each other's functions. | |
// the server can request of the client/ client of server and ... even deeper chains if required. | |
// | |
var dnode = require("dnode"); | |
var clients = []; | |
// "server" | |
function serverFactory() { // every client connection gets a new instance of a dnode stream | |
// Well this is dirty.... | |
// I don't know if client being set in the .on handler races the ability for them to be called in the dnode constructor | |
// might not be required at all. | |
var setClient; | |
var client; = new Promise(function(resolve, reject) { | |
setClient = resolve; | |
}); | |
var server = dnode({ | |
foo: function(arg1, arg2) { | |
setTimeout(function() { | |
arg1('foo 1'); | |
}, 1000); | |
setTimeout(function() { | |
arg2('foo 2'); | |
}, 2000); | |
}, | |
feedback: function(clientMsg) {// in here we need access to the client so .. | |
console.log("feedback step 2"); | |
client.then(function(client) { | |
client.msg('Message from feedback' + clientMsg); | |
}); | |
} | |
}); | |
server.on('remote', function(client) { | |
console.log("Got a client connection"); | |
setClient(client); | |
}); | |
client.then(function(client) { | |
clients.push(client); | |
}); | |
return server; | |
} | |
//technically you need to track disconnects.... and remove them | |
function clientFactory(name) { | |
var setServer; | |
var server = new Promise(function(resolve, reject) { | |
setServer = resolve; | |
}); | |
var client = dnode({ | |
msg: function(msg) { | |
console.log("This is a message broadcast from the server to client",name, msg); | |
}, | |
feedback: function(msg) { // in here we tell the server to do a thing! | |
console.log("feedback step 1"); | |
// or ... | |
// (await server).feedback('test'); | |
server.then(function(server) { | |
server.feedback(msg); | |
}); | |
} | |
}); | |
client.on('remote', function(server) { | |
console.log("Client stream connected"); | |
setServer(server); | |
}); | |
server.then(function(server) { | |
// let start this game! | |
server.foo( | |
function(msg) { | |
console.log("Result 1", msg); | |
}, | |
function(msg) { | |
console.log("Result 2", msg); | |
} | |
); | |
}); | |
return client; | |
} | |
var anode = clientFactory('Node A'); | |
var bnode = clientFactory('Node B'); | |
// "client -> server -> client" - a connection forces the 'remote' event to show up on the client. | |
anode.pipe(serverFactory()).pipe(anode); | |
bnode.pipe(serverFactory()).pipe(bnode); | |
// Lets tell the clients something... | |
setTimeout(function() { | |
console.log("Client Messages"); | |
clients[0].msg("Server message!!"); | |
clients[1].msg("Another message!!"); | |
}, 500); | |
setTimeout(function() { | |
clients[1].feedback('test message'); | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment