Created
November 30, 2012 23:38
-
-
Save oost/4179532 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
<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> |
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
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); | |
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
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