-
-
Save nczz/6303746 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, routes = require('./routes') | |
, socket = require('socket.io') | |
, RedisStore = socket.RedisStore | |
, cluster = require('cluster'); | |
var app = module.exports = express.createServer(); | |
// Configuration | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
/** | |
* Handle `request` event. | |
*/ | |
app.on('request', function(req, res) { | |
if (!req.url.match(/^\/socket\.io\/1/)) { | |
return; | |
} | |
console.log('*** request ***'); | |
console.log('handle request on %s', process.env.NODE_WORKER_ID || process.pid); | |
console.log({ handshaken: io.handshaken, open: io.open, connected: io.connected }); | |
}); | |
/** | |
* Handle `upgrade` event. | |
*/ | |
app.on('upgrade', function(req, res) { | |
console.log('*** upgrade ***'); | |
console.log('handle upgrade on %s', process.env.NODE_WORKER_ID || process.pid); | |
console.log(req.url); | |
console.log(req.headers); | |
console.log({ handshaken: io.handshaken, open: io.open, connected: io.connected }); | |
}); | |
// Routes | |
app.get('/', routes.index); | |
if (!process.env.NODE_WORKER_ID) { | |
var io = socket.listen(app, { store: new RedisStore() }) | |
, handleClient = io.handleClient; | |
/** | |
* Configure Socket.IO | |
*/ | |
io.set('transports', ['websocket']); | |
/** | |
* Hook `handleClient` method. | |
*/ | |
io.handleClient = function(data, req) { | |
console.log('*** handleClient ***'); | |
console.log(data); | |
handleClient.apply(io, arguments); | |
}; | |
io.sockets.on('connection', function(client) { | |
var timerId = setInterval(function() { | |
client.emit('ping', process.env.NODE_WORKER_ID || process.pid); | |
}, 1000); | |
}); | |
/** | |
* Boot. | |
*/ | |
app.listen(process.env.PORT || 3000); | |
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); | |
} |
This file contains 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
/** | |
* Module dependencies. | |
*/ | |
var cluster = require('cluster') | |
, app = require('./app'); | |
/** | |
* Set number of worker process. | |
* Default is number of CPUs. | |
*/ | |
var workers = process.env.WORKERS || require('os').cpus().length; | |
/** | |
* Start cluster. | |
*/ | |
if (cluster.isMaster) { | |
/** | |
* Fork process. | |
*/ | |
console.log('start cluster with %s workers', workers); | |
for (var i = 0; i < workers; ++i) { | |
var worker = cluster.fork(); | |
console.log('worker %s started.', worker.pid); | |
} | |
/** | |
* Restart process. | |
*/ | |
cluster.on('death', function(worker) { | |
console.log('worker %s died. restart...', worker.pid); | |
cluster.fork(); | |
}); | |
} else { | |
/** | |
* Start worker. | |
*/ | |
app.listen(process.env.PORT || 3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment