Skip to content
Browse files

test

  • Loading branch information...
1 parent 7fc6a91 commit 5da20ea2673dec93f778e69dc34c2d3666377742 @dvv committed Mar 15, 2011
Showing with 35 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. +3 −2 index.coffee
  3. +12 −11 index.js
  4. +19 −0 test.js
View
1 .gitignore
@@ -0,0 +1 @@
+.ipc
View
5 index.coffee
@@ -31,7 +31,7 @@ framing = (chunk) ->
node cluster factory, takes options:
-options.host - host to bind server to = *
+options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80
options.connections - listener capacity = 1024
@@ -75,6 +75,7 @@ module.exports = (options = {}) ->
# options
options.port ?= 80
+ options.host ?= '0.0.0.0'
nworkers = options.workers or require('os').cpus().length
options.ipc ?= '.ipc'
@@ -215,7 +216,7 @@ module.exports = (options = {}) ->
#
netBinding = process.binding 'net'
socket = netBinding.socket 'tcp' + (if netBinding.isIP(options.host) is 6 then 6 else 4)
- netBinding.bind socket, options.port
+ netBinding.bind socket, options.port, options.host
netBinding.listen socket, options.connections or 1024
#
View
23 index.js
@@ -29,7 +29,7 @@ framing = function(chunk) {
node cluster factory, takes options:
-options.host - host to bind server to = *
+options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80
options.connections - listener capacity = 1024
@@ -67,15 +67,16 @@ options.ssl.caCerts
*/
module.exports = function(options) {
- var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, server, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4;
+ var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, server, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4, _ref5;
if (options == null) {
options = {};
}
net = require('net');
fs = require('fs');
(_ref = options.port) != null ? _ref : options.port = 80;
+ (_ref2 = options.host) != null ? _ref2 : options.host = '0.0.0.0';
nworkers = options.workers || require('os').cpus().length;
- (_ref2 = options.ipc) != null ? _ref2 : options.ipc = '.ipc';
+ (_ref3 = options.ipc) != null ? _ref3 : options.ipc = '.ipc';
if (process.env._NODE_WORKER_FOR_) {
process.log = function() {
var args;
@@ -149,7 +150,7 @@ module.exports = function(options) {
};
netBinding = process.binding('net');
socket = netBinding.socket('tcp' + (netBinding.isIP(options.host) === 6 ? 6 : 4));
- netBinding.bind(socket, options.port);
+ netBinding.bind(socket, options.port, options.host);
netBinding.listen(socket, options.connections || 1024);
if (process.getuid() === 0) {
if (options.uid) {
@@ -165,18 +166,18 @@ module.exports = function(options) {
workers = {};
args = options.args || process.argv;
env = {};
- _ref3 = process.env;
- for (k in _ref3) {
- if (!__hasProp.call(_ref3, k)) continue;
- v = _ref3[k];
- env[k] = v;
- }
- _ref4 = options.env || {};
+ _ref4 = process.env;
for (k in _ref4) {
if (!__hasProp.call(_ref4, k)) continue;
v = _ref4[k];
env[k] = v;
}
+ _ref5 = options.env || {};
+ for (k in _ref5) {
+ if (!__hasProp.call(_ref5, k)) continue;
+ v = _ref5[k];
+ env[k] = v;
+ }
spawnWorker = function() {
var worker;
env._NODE_WORKER_FOR_ = process.pid;
View
19 test.js
@@ -0,0 +1,19 @@
+var server = require('./')({
+ port: 3000,
+ repl: true,
+ workers: 4
+});
+if (server) { // worker process
+ // HTTP(S) server instance for further tuning
+ server.on('request', function(req, res){
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('HELLO FROM WORKER ' + process.pid);
+ });
+ // inter-workers message arrives
+ process.on('message', function(message){
+ console.log(JSON.stringify(message));
+ });
+} else { // master process
+ // broadcast a message
+ process.publish({sos: 'to all, all, all'});
+}

0 comments on commit 5da20ea

Please sign in to comment.
Something went wrong with that request. Please try again.