Skip to content

Instantly share code, notes, and snippets.

@tuchida
Created January 10, 2014 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuchida/8347864 to your computer and use it in GitHub Desktop.
Save tuchida/8347864 to your computer and use it in GitHub Desktop.
Server-Sent Eventsテスト
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.type('text/html');
res.send('<script>' +
'new EventSource(\'/event/a\').addEventListener(\'message\',function(event) {' +
'document.body.appendChild(document.createElement(\'div\')).textContent = event.data;' +
'});' +
'</script>');
});
app.get('/event/:id', function(req, res) {
res.type('text/event-stream');
setInterval(function() {
// 任意の文字列を返している、という想定
res.write('data: <script>alert(1)</script>\n\n');
}, 1000);
});
app.listen(3000);
{
"name": "sse-test",
"scripts": {
"run": "node app.js"
},
"dependencies": {
"express": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment