Permalink
Browse files

Merge pull request #56 from timoxley/feature/close

Add listening/close events, and a close() function
  • Loading branch information...
baalexander committed Sep 4, 2012
2 parents cd4c57c + 0c3de93 commit a9d9ab92628de90753ff9e2f008350277b2c2207
Showing with 37 additions and 14 deletions.
  1. +12 −4 lib/server.js
  2. +4 −4 lib/xmlrpc.js
  3. +21 −6 test/server_test.js
View
@@ -20,12 +20,12 @@ var http = require('http')
* otherwise false.
* @return {Server}
*/
-function Server(options, isSecure) {
+function Server(options, isSecure, onListening) {
if (false === (this instanceof Server)) {
return new Server(options, isSecure)
}
-
+ onListening = onListening || function() {}
var that = this
// If a string URI is passed in, converts to URI fields
@@ -58,9 +58,17 @@ function Server(options, isSecure) {
}
})
}
- var httpServer = isSecure ? https.createServer(options, handleMethodCall)
+
+ this.httpServer = isSecure ? https.createServer(options, handleMethodCall)
: http.createServer(handleMethodCall)
- httpServer.listen(options.port, options.host)
+
+ process.nextTick(function() {
+ this.httpServer.listen(options.port, options.host, onListening)
+ }.bind(this))
+ this.close = function(callback) {
+ this.httpServer.once('close', callback)
+ this.httpServer.close()
+ }.bind(this)
}
// Inherit from EventEmitter to emit and listen
View
@@ -38,8 +38,8 @@ xmlrpc.createSecureClient = function(options) {
* @return {Server}
* @see Server
*/
-xmlrpc.createServer = function(options) {
- return new Server(options, false)
+xmlrpc.createServer = function(options, callback) {
+ return new Server(options, false, callback)
}
/**
@@ -51,7 +51,7 @@ xmlrpc.createServer = function(options) {
* @return {Server}
* @see Server
*/
-xmlrpc.createSecureServer = function(options) {
- return new Server(options, true)
+xmlrpc.createSecureServer = function(options, callback) {
+ return new Server(options, true, callback)
}
View
@@ -87,17 +87,32 @@ vows.describe('Server').addBatch({
'with an unknown method': {
topic: function() {
var server = new Server({ port: 9996, path: '/'}, false)
- server.on('NotFound', this.callback);
- setTimeout(function () {
+
+ server.on('NotFound', this.callback);
+ setTimeout(function () {
var options = { host: 'localhost', port: 9996, path: '/' }
- var client = new Client(options, false)
- client.methodCall('testMethod', null, function() { })
- }, 500)
+ var client = new Client(options, false)
+ client.methodCall('testMethod', null, function() { })
+ }, 500)
}
, 'return 404' : function (method, params) {
assert.equal(method, 'testMethod');
- assert.deepEqual(params, []);
+ assert.deepEqual(params, []);
}
}
}
+, 'close()': {
+ topic: function() {
+ console.log()
+ var that = this
+ var server = new Server({ port: 9995, path: '/'}, false, function() {
+ server.close(function() {
+ var server2 = new Server({ port: 9995, path: '/'}, false, that.callback)
+ })
+ })
+ }
+ , 'allows new connections on same port': function (error) {
+ assert.ifError(error)
+ }
+ }
}).export(module)

0 comments on commit a9d9ab9

Please sign in to comment.