sse.html 853 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  5. <title>EventSource test</title>
  6. </head>
  7. <body>
  8. <h1>EventSource test</h1>
  9. <ul></ul>
  10. <script type="text/javascript">
  11. var logger = document.getElementsByTagName('ul')[0],
  12. socket = new EventSource('/');
  13. var log = function(text) {
  14. logger.innerHTML += '<li>' + text + '</li>';
  15. };
  16. socket.onopen = function() {
  17. log('OPEN');
  18. };
  19. socket.onmessage = function(event) {
  20. log('MESSAGE: ' + event.data);
  21. };
  22. socket.addEventListener('update', function(event) {
  23. log('UPDATE(' + event.lastEventId + '): ' + event.data);
  24. });
  25. socket.onerror = function(event) {
  26. log('ERROR: ' + event.message);
  27. };
  28. </script>
  29. </body>
  30. </html>