| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <script type="text/javascript"> |
| 5 | function ready() { |
| 6 | if (!!window.EventSource) { |
| 7 | setupEventSource(); |
| 8 | } else { |
| 9 | document.getElementById('status').innerHTML = |
| 10 | "Sorry but your browser doesn't support the EventSource API"; |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | function setupEventSource() { |
| 15 | var source = new EventSource('/eventsource'); |
| 16 | |
| 17 | source.addEventListener('message', function(event) { |
| 18 | addStatus("server sent the following: '" + event.data + "'"); |
| 19 | }, false); |
| 20 | |
| 21 | source.addEventListener('open', function(event) { |
| 22 | addStatus('eventsource connected.') |
| 23 | }, false); |
| 24 | |
| 25 | source.addEventListener('error', function(event) { |
| 26 | if (event.eventPhase == EventSource.CLOSED) { |
| 27 | addStatus('eventsource was closed.') |
| 28 | } |
| 29 | }, false); |
| 30 | } |
| 31 | |
| 32 | function addStatus(text) { |
| 33 | var date = new Date(); |
| 34 | document.getElementById('status').innerHTML |
| 35 | = document.getElementById('status').innerHTML |
| 36 | + date + ": " + text + "<br/>"; |
| 37 | } |
| 38 | </script> |
| 39 | </head> |
| 40 | <body onload="ready();"> |
| 41 | Hi! |
| 42 | <div id="status"></div> |
| 43 | </body> |
| 44 | </html> |