Last active
December 29, 2015 14:39
-
-
Save niahmiah/7685529 to your computer and use it in GitHub Desktop.
Demo of sockjs connections not closing when proxied
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
<!doctype html> | |
<html><head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> | |
<style> | |
.box { | |
width: 300px; | |
float: left; | |
margin: 0 20px 0 20px; | |
} | |
.box div, .box input { | |
border: 1px solid; | |
-moz-border-radius: 4px; | |
border-radius: 4px; | |
width: 100%; | |
padding: 0px; | |
margin: 5px; | |
} | |
.box div { | |
border-color: grey; | |
height: 300px; | |
overflow: auto; | |
} | |
.box input { | |
height: 30px; | |
} | |
h1 { | |
margin-left: 30px; | |
} | |
body { | |
background-color: #F0F0F0; | |
font-family: "Arial"; | |
} | |
</style> | |
</head><body lang="en"> | |
<h1>SockJS Echo example</h1> | |
<div id="first" class="box"> | |
<div></div> | |
<form><input autocomplete="off" value="Type here..."></input></form> | |
</div> | |
<script> | |
var sockjs_url = '/echo'; | |
var sockjs = new SockJS(sockjs_url); | |
$('#first input').focus(); | |
var div = $('#first div'); | |
var inp = $('#first input'); | |
var form = $('#first form'); | |
var print = function(m, p) { | |
p = (p === undefined) ? '' : JSON.stringify(p); | |
div.append($("<code>").text(m + ' ' + p)); | |
div.append($("<br>")); | |
div.scrollTop(div.scrollTop()+10000); | |
}; | |
sockjs.onopen = function() {print('[*] open', sockjs.protocol);}; | |
sockjs.onmessage = function(e) {print('[.] message', e.data);}; | |
sockjs.onclose = function() {print('[*] close');}; | |
form.submit(function() { | |
print('[ ] sending', inp.val()); | |
sockjs.send(inp.val()); | |
inp.val(''); | |
return false; | |
}); | |
</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
{ | |
"name": "sockjs-echo", | |
"version": "0.0.0-unreleasable", | |
"dependencies": { | |
"node-static": "0.5.9", | |
"sockjs": "*", | |
"http-proxy":"git://github.com/nodejitsu/node-http-proxy#caronte" | |
} | |
} |
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'), | |
sockjs = require('sockjs'), | |
node_static = require('node-static'); | |
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js", websocket:false}; | |
console.log("Proxy server listening on port 8000"); | |
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); | |
console.log("Web server listening on port 9000"); | |
var server = http.createServer(sockjs_opts); | |
server.addListener('request', function(req, res) { | |
static_directory.serve(req, res); | |
}); | |
server.addListener('upgrade', function(req,res){ | |
res.end(); | |
}); | |
var echo = sockjs.createServer(); | |
echo.on('connection', function(conn) { | |
conn.on('data', function(message) { | |
conn.write(message); | |
}); | |
conn.on('close', function() { | |
console.log("socket closed"); | |
}); | |
}); | |
var static_directory = new node_static.Server(__dirname); | |
echo.installHandlers(server, {prefix:'/echo'}); | |
server.listen(9000, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment