Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Koa = require('koa');
- const Router = require('koa-router');
- const app = new Koa();
- const router = new Router();
- router.get('/stream', (ctx, next) => {
- ctx.set({
- 'Access-Control-Allow-Origin': '*',
- 'Content-Type': 'text/event-stream',
- 'Cache-Control': 'no-cache',
- Connection: 'Keep-Alive',
- });
- const id = new Date().toLocaleTimeString();
- ctx.res.write(`id: ${id}'n`);
- ctx.res.write(`data: CONNECTION ESTABLISHED)}nn`);
- next();
- });
- app.use(router.routes());
- app.listen(8080, () => {
- console.log('Listening on port 8080');
- });
- constructor(props) {
- super(props);
- this.state = {
- source: new EventSource("http://localhost:8080/stream"),
- };
- }
Add Comment
Please, Sign In to add comment