Permalink
Browse files

let .close() work for servers and update docs for the new upnode.list…

…en usage in the examples
  • Loading branch information...
1 parent 9f73ece commit 5c078eb9e010ad9bfed59056ba281e134ac2b10a @substack substack committed Mar 24, 2012
Showing with 40 additions and 34 deletions.
  1. +1 −5 README.markdown
  2. +39 −29 index.js
View
@@ -16,13 +16,11 @@ Write a dnode server as usual but enable the upnode ping middleware with
server.js:
``` js
-var dnode = require('dnode');
var upnode = require('upnode');
var server = dnode(function (client, conn) {
this.time = function (cb) { cb(new Date().toString()) };
});
-server.use(upnode.ping);
server.listen(7000);
```
@@ -97,10 +95,9 @@ Suppose we have a simple dnode server with a `beep` function protected behind an
server.js:
``` js
-var dnode = require('dnode');
var upnode = require('upnode');
-var server = dnode(function (client, conn) {
+var server = upnode(function (client, conn) {
this.auth = function (user, pass, cb) {
if (user === 'moo' && pass === 'hax') {
cb(null, {
@@ -110,7 +107,6 @@ var server = dnode(function (client, conn) {
else cb('ACCESS DENIED')
};
});
-server.use(upnode.ping);
server.listen(7000);
```
View
@@ -2,37 +2,47 @@ var dnode = require('dnode');
var EventEmitter = require('events').EventEmitter;
var upnode = module.exports = function (cons) {
- return {
- connect : function () {
- var args = [].slice.call(arguments);
- var up = createConnectionUp();
- return connect.apply(null, [ up, cons ].concat(args));
- },
- listen : function () {
- var args = [].slice.call(arguments);
- var server = dnode(cons);
- server.use(upnode.ping);
- server.use(function (remote, conn) {
- var iv = setInterval(function () {
- if (typeof remote.ping === 'function') {
- var to = setTimeout(function () {
- conn.end();
- }, 10 * 10000);
-
- remote.ping(function () {
- clearTimeout(to);
- });
- }
- }, 10 * 1000);
-
- conn.once('end', function () {
- clearInterval(iv);
- });
+ var self = {};
+ self.connect = function () {
+ var args = [].slice.call(arguments);
+ var up = createConnectionUp();
+ return connect.apply(null, [ up, cons ].concat(args));
+ };
+
+ self.listen = function () {
+ var args = [].slice.call(arguments);
+ var server = dnode(cons);
+ server.use(upnode.ping);
+ server.use(function (remote, conn) {
+ var iv = setInterval(function () {
+ if (typeof remote.ping === 'function') {
+ var to = setTimeout(function () {
+ conn.end();
+ }, 10 * 10000);
+
+ remote.ping(function () {
+ clearTimeout(to);
+ });
+ }
+ }, 10 * 1000);
+
+ conn.once('end', function () {
+ clearInterval(iv);
});
- server.listen.apply(server, args);
- return server;
- },
+ });
+ server.listen.apply(server, args);
+
+ if (!self.close) {
+ self._servers = [];
+ self.close = function () {
+ self._servers.forEach(function (s) { s.close() });
+ };
+ }
+ self._servers.push(server);
+ return server;
};
+
+ return self;
};
function createConnectionUp () {

0 comments on commit 5c078eb

Please sign in to comment.