Created
          January 10, 2014 06:40 
        
      - 
      
- 
        Save tuchida/8347864 to your computer and use it in GitHub Desktop. 
    Server-Sent Eventsテスト
  
        
  
    
      This file contains hidden or 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 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); | 
  
    
      This file contains hidden or 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": "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