Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Created June 6, 2014 05:46
Show Gist options
  • Save jcheng5/d7ba5dc2ac4de9bc7aac to your computer and use it in GitHub Desktop.
Save jcheng5/d7ba5dc2ac4de9bc7aac to your computer and use it in GitHub Desktop.
Demonstration of @sockjs/sockjs-node issue #143

Put these two files in a directory and run npm install sockjs, then point a browser at http://localhost:9999/.

<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<script>
var sock = new SockJS('http://localhost:9999/heartbeat-test');
sock.onopen = function() {
document.body.innerHTML = ('Connection opened');
};
sock.onclose = function() {
document.body.innerHTML = ('Connection closed');
};
</script>
</head>
<body>
</body>
</html>
var fs = require('fs');
var http = require('http');
var sockjs = require('sockjs');
var echo = sockjs.createServer({
response_limit: 100,
heartbeat_delay: 1000,
websocket: false
});
echo.on('connection', function(conn) {
// This message is >= response_limit so it will cause the
// current response to be closed.
conn.write(Array(101).join('h'));
// This write happens right away so it goes into the send
// buffer. Because there's something in the send buffer,
// when register() is called, the message is sent but the
// heartbeat is not scheduled.
conn.write('i');
conn.on('close', function() {
console.log('closed');
});
});
var server = http.createServer();
server.on('connection', function(conn) {
conn.setTimeout(2000);
});
server.on('request', function(request, response) {
if (request.url === '/') {
var body = fs.readFileSync('index.html')
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/html'
});
response.end(body);
}
});
echo.installHandlers(server, {prefix:'/heartbeat-test'});
server.listen(9999, '0.0.0.0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment