Created
November 18, 2018 00:15
-
-
Save nomoney4me/c4d5bfce7c94cf83de2acfc500c13ba9 to your computer and use it in GitHub Desktop.
sse-practice
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
| <!<!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script | |
| src="https://code.jquery.com/jquery-3.3.1.min.js" | |
| integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" | |
| crossorigin="anonymous"></script> | |
| </head> | |
| <body> | |
| <div id="data"></div> | |
| <script> | |
| $(document).ready(function() { | |
| var es = new EventSource('/stream') | |
| es.onmessage = function(e) { | |
| $("#data").append(`<li>${e.data}</li>`) | |
| } | |
| es.addEventListener('msg', function(e) { | |
| // console.log(e.) | |
| }) | |
| }) | |
| </script> | |
| </body> | |
| </html> |
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
| const express = require('express') | |
| , app = express() | |
| , SSE = require('express-sse') | |
| , sse = new SSE() | |
| app.use(express.static('./client')) | |
| app.use('/stream', sse.init); | |
| setInterval(() => { | |
| sse.send('hello') | |
| }, 3000) | |
| app.listen(3001, console.log(`listening on 3001`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment