client.html 483 B

1234567891011121314151617181920212223242526
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Server Sent Event</title>
  6. </head>
  7. <body>
  8. <div id="event-data"></div>
  9. </body>
  10. <script>
  11. const $stream = new EventSource("/stream");
  12. const $log = document.querySelector('#event-data')
  13. $stream.addEventListener("message", function (e) {
  14. log(e.data)
  15. });
  16. function log(msg) {
  17. $log.innerHTML += `<p>消息: ${msg}</p>`
  18. $log.scrollTop += 1000
  19. }
  20. </script>
  21. </html>