-
-
Save adamrights/8781402 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') | |
, cluster = require('cluster'); | |
var app = module.exports = express.createServer() | |
, io = require('./socket')(app); | |
// 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 && !module.parent) { | |
/** | |
* 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
{ | |
"name": "application-name" | |
, "version": "0.0.1" | |
, "private": true | |
, "dependencies": { | |
"express": "2.5.8" | |
, "jade": ">= 0.0.1" | |
, "socket.io": ">= 0.0.1" | |
, "bouncy": ">= 0.0.1" | |
} | |
} |
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 bouncy = require('bouncy'); | |
/** | |
* Proxy server. | |
*/ | |
bouncy(function(req, bounce) { | |
if (0 === Math.round(Math.random()) % 2) { | |
bounce('localhost', 10000); | |
console.log('route to 10000'); | |
} else { | |
bounce('localhost', 10001); | |
console.log('route to 10001'); | |
} | |
}).listen(8000); | |
console.log('Proxy server listening on port %s', 8000); |
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 socket = require('socket.io') | |
, RedisStore = socket.RedisStore; | |
module.exports = function(app) { | |
/** | |
* Attach HTTP server. | |
*/ | |
var io = socket.listen(app, { store: new RedisStore({ /* http://bit.ly/zzicYN */ }) }) | |
, handleClient = io.handleClient; | |
/** | |
* Configure Socket.IO | |
*/ | |
io.set('transports', ['websocket']); | |
/** | |
* Hook `handleClient` method. | |
*/ | |
io.handleClient = function(data, req) { | |
console.log('*** handleClient ***'); | |
console.log(data); | |
return handleClient.apply(io, arguments); | |
}; | |
/** | |
* Routes. | |
*/ | |
io.sockets.on('connection', function(client) { | |
var timerId = setInterval(function() { | |
client.emit('ping', process.env.NODE_WORKER_ID || process.pid); | |
}, 1000); | |
}); | |
return io; | |
}; |
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
$ redis-server `brew --prefix`/etc/redis.conf & | |
$ PORT=10000 node cluster & | |
$ PORT=10001 node cluster & | |
$ node proxy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment