Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created July 28, 2014 08:59
Show Gist options
  • Save kyo-ago/6e9e46791ea61c592831 to your computer and use it in GitHub Desktop.
Save kyo-ago/6e9e46791ea61c592831 to your computer and use it in GitHub Desktop.
server sent event test
var SSE = require('sse')
, http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><head><title></title></head><body>');
res.write('<script src="https://raw.githubusercontent.com/Yaffle/EventSource/master/eventsource.js"></script><script>');
res.write('var es = new EventSource("/sse");es.onmessage = function (event) {alert(event.data);};');
res.write('</script></body></html>');
res.end();
});
var count = 0;
server.listen(8765, function() {
var sse = new SSE(server);
sse.on('connection', function(client) {
setInterval(function () {
count++;
console.log('send', count);
client.send(count + Array(10000).join(',') + ';');
setTimeout(function () {
client.send(count + Array(5000).join(',') + ';');
}, 1000);
}, 10000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment