Created
February 27, 2012 15:39
-
-
Save Jxck/1924757 to your computer and use it in GitHub Desktop.
Test WebSocket/Socket.IO over ELB
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
node_modules |
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') | |
; | |
var addresses = [ | |
{ host: 'localhost', port: 3000 } | |
, { host: 'localhost', port: 3001 } | |
, { host: 'localhost', port: 3002 } | |
]; | |
function getTarget(index) { | |
index = index || Math.floor(Math.random() * 3); | |
var target = addresses[index]; | |
return { host: target.host, port: target.port }; | |
} | |
var server = http.createServer(function(req, res) { | |
// var proxy = new httpProxy.HttpProxy({ target: getTarget() }); | |
var proxy = new httpProxy.HttpProxy({ target: addresses[0] }); | |
proxy.proxyRequest(req, res); | |
}); | |
server.on('upgrade', function(req, socket, head) { | |
// var proxy = new httpProxy.HttpProxy({ target: getTarget() }); | |
var proxy = new httpProxy.HttpProxy({ target: addresses[1] }); | |
proxy.proxyWebSocketRequest(req, socket, head); | |
}); | |
server.listen(4000); |
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" | |
, "ejs": ">= 0.0.1" | |
, "socket.io": "0.9.1-1" | |
, "ws": "0.4.7" | |
, "redis": "0.7.1" | |
, "http-proxy": "0.8.0" | |
} | |
} |
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
// Client | |
var socket = io.connect(); | |
function dispay(message) { | |
document.getElementById('console').innerText += message + '\n'; | |
}; | |
socket.on('connect', function() { | |
dispay('connect via ' + socket.socket.transport.name); | |
socket.emit('msg send', 'hello'); | |
socket.on('msg push', function (message) { | |
dispay(message); | |
}); | |
socket.on('disconnect', function(){ | |
dispay('disconnect'); | |
}); | |
}); | |
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 express = require('express') | |
, io = require('socket.io') | |
, redis = require('redis') | |
, port = process.argv[2] || 3000 | |
; | |
var app = module.exports = express.createServer() | |
, SocketRedisStore = io.RedisStore | |
, io = io.listen(app) | |
; | |
app.configure(function() { | |
app.use(express.cookieParser()); | |
app.use(express.session({ secret: 'elb websocket test' })); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
io.configure(function() { | |
io.set('store', new SocketRedisStore()); | |
}); | |
io.sockets.on('connection', function(socket) { | |
console.log('connect via', socket.transport); | |
socket.on('msg send', function(msg) { | |
socket.emit('msg push', msg + ' from ' + process.pid); | |
}); | |
}); | |
app.listen(port); | |
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
<html> | |
<head> | |
<title>Socket.IO Sample</title> | |
</head> | |
<body> | |
<div id="console"></div> | |
<script type="text/javascript" src="socket.io/socket.io.js"></script> | |
<script type="text/javascript" src="sio-client.js"></script> | |
</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 express = require('express') | |
, io = require('socket.io') | |
; | |
var app = module.exports = express.createServer() | |
, io = io.listen(app); | |
app.configure(function() { | |
app.use(express.cookieParser()); | |
app.use(express.session({ secret: 'elb websocket test' })); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
// io.configure(function() { | |
// io.set('transports', ['xhr-polling']); | |
// }); | |
io.sockets.on('connection', function(socket) { | |
console.log('connect via', socket.transport); | |
socket.on('msg send', function(msg) { | |
socket.emit('msg push', msg + ' from ' + process.pid); | |
}); | |
}); | |
app.listen(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
// Client | |
var ws = new WebSocket('ws://localhost:4000'); | |
function dispay(message) { | |
document.getElementById('console').innerText += message + '\n'; | |
}; | |
ws.onopen = function() { | |
dispay('connect via ' + 'websocket(ws)'); | |
ws.send('hello'); | |
ws.onmessage = function(ev) { | |
dispay(ev.data); | |
}; | |
ws.onclose = function() { | |
dispay('disconnect'); | |
}; | |
}; |
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> | |
<title>WS Sample</title> | |
</head> | |
<body> | |
<div id="console"></div> | |
<script type="text/javascript" src="ws-client.js"></script> | |
</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 express = require('express') | |
, WebSocketServer = require('ws').Server | |
, wss = new WebSocketServer({ port: 4000 }); | |
; | |
var app = module.exports = express.createServer(); | |
app.configure(function() { | |
app.use(express.cookieParser()); | |
app.use(express.session({ secret: 'your secret here' })); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
wss.on('connection', function(ws) { | |
console.log('connected'); | |
ws.on('message', function(msg) { | |
ws.send(msg + ' from ' + process.pid); | |
}); | |
}); | |
app.listen(3000); | |
console.log('Express server listening on port %d in %s mode', app.address().port, app.settings.env); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your tutorial.