dnode
the DNode prototype is the main part of this module. it is used wrap an object in the DNode protocol.
the DNode prototype is exported as modules.exports = DNode
and as modules.exports.DNode = DNode
so it may be invoked as both:
DNode = require('dnode')
or
DNode = require('dnode').DNode
DNode(wrapper)
wrap and object in DNode
so that it can be used as a server or as a client (see DNode.listen
and DNode.connect
respectively)
wrapper
can be either an object, or a constructor for an object.
if wrapper
is a constructor, then a new instance with be created each time a connection is made.
the constructor is called with the arguments new wrapper(remote,connection)
where remote a representation of the wrapper from
the connecting DNode
, and connection
is the connection object (see dnode/lib/conn.js
)
as of version 0.3.6 this is the only way to get a hold of a connection object.
if wrapper
is a object, the same object will be used for every connection.
.listen(port,options,server)
create a server from a DNode
instance. arguments may be provided in any order, and are parsed by their type. port
is the only non-optional argument.
if server is an instance of http.Server
, net.Server
or io.Listener
it will be used to host the DNode server. (I think, I havn't actually tried this yet, and the only example I've seen in dnode/test/stream.js
- Dominic)
options is a hash of each argument, but they are named. if port is in args it does not need to be passed directly to listen.
.connect(port,options,server,onConnect)
connects to a DNode server on the given port.
the arguments have the same behaviour as listen except there is a callback for when the connection to the server is returned. which will be called like this: onConnect(remote)
where remote
is a repesentation of the wrapped instance used to start the server.
.end
end all connections from a server. attempting to .end() a client will throw an error.
.close
synonym for end.
.withStream(stream,opts,block)
I don't fully understand this one yet, can you help, James? -Dominic
.on(eventname, listener)
on method from EventEmitter
DNode generates the following events + 'ready' generated by a server or client once it is able to recieve/make a connection.
I vote for including a few more events, to make the interface more flexible - Dominic.
sync(function)
wraps a sync method in a another function which callsback with the return value: function ten (){ return 10 };
tenAsync = DNode.sync(ten)
tenAsync(function(r) {
assert.equal(r,10);
});
connect(port,...)
calls DNode().connect(port,...)
i.e. creates empty DNode instance and connects to server on given port.
listen(port,...)
calls DNode().listen(port,...)
i.e. creates an empty DNode instance starts as a server listening on port
expose(obj,method)
tell DNode to show an inherited method across the connection. DNode does not do this by default, and must be told to do so explicitly. this is a feature.
usuage:
DNode(function Class1 (client,conn){
DNode.expose(this,'methodName')
//etc
}).listen(6060)