Skip to content

Instantly share code, notes, and snippets.

@oost
Created November 30, 2012 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oost/4179532 to your computer and use it in GitHub Desktop.
Save oost/4179532 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
document.write(data.hello);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
</body>
</html>
var http = require('http'),
httpProxy = require('http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
{
host: 'localhost',
port: 9000
},
{
host: 'localhost',
port: 9001
}
];
var proxies = addresses.map(function (target) {
return new httpProxy.HttpProxy({
target: target
});
});
function nextProxy() {
var proxy = proxies.shift();
proxies.push(proxy);
return proxy;
}
var server = http.createServer(function (req, res) {
nextProxy().proxyRequest(req, res);
});
server.on('upgrade', function(req, socket, head) {
nextProxy().proxyWebSocketRequest(req, socket, head);
});
server.listen(8080);
var http = require('http'),
argv = require('optimist').argv,
port = argv.port,
socketio = require('socket.io'),
fs = require('fs'),
redis = require('redis');
var server = http.createServer(function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
console.log('Served from ' + port);
});
}).listen(port);
var io = socketio.listen(server);
var pub = redis.createClient();
var sub = redis.createClient();
var store = redis.createClient();
var RedisStore = require('socket.io/lib/stores/redis');
io.configure( function(){
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
io.set('store', new RedisStore({redisPub:pub, redisSub:sub, redisClient:store, redis: redis}));
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log('Server running at http://127.0.0.1:' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment