Created
September 17, 2014 20:46
-
-
Save remy/50f1758a74242d51a90f to your computer and use it in GitHub Desktop.
Sample node server that sends chunked script tags.
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'); | |
var server = http.createServer(function (req, res) { | |
res.writeHead(200, { 'content-type': 'text/html' }); | |
res.write('<script>console.log("this is the start of the stream...")</script>'); | |
var timer = setInterval(function () { | |
if (!res.connection || !res.connection.writable) { | |
try { res.end(); } catch (e) {} | |
clearInterval(timer); | |
console.log('closed'); | |
return; | |
} | |
res.write('<script>console.log("and now more messages...")</script>'); | |
}, 2000); | |
// importantly, res.end() is never called. | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment