Guest User

Untitled

a guest
Nov 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. const Koa = require('koa');
  2. const Router = require('koa-router');
  3.  
  4. const app = new Koa();
  5. const router = new Router();
  6.  
  7. router.get('/stream', (ctx, next) => {
  8. ctx.set({
  9. 'Access-Control-Allow-Origin': '*',
  10. 'Content-Type': 'text/event-stream',
  11. 'Cache-Control': 'no-cache',
  12. Connection: 'Keep-Alive',
  13. });
  14.  
  15. const id = new Date().toLocaleTimeString();
  16. ctx.res.write(`id: ${id}'n`);
  17. ctx.res.write(`data: CONNECTION ESTABLISHED)}nn`);
  18. next();
  19. });
  20.  
  21. app.use(router.routes());
  22.  
  23. app.listen(8080, () => {
  24. console.log('Listening on port 8080');
  25. });
  26.  
  27. constructor(props) {
  28. super(props);
  29.  
  30. this.state = {
  31. source: new EventSource("http://localhost:8080/stream"),
  32. };
  33. }
Add Comment
Please, Sign In to add comment