Skip to content

Commit

Permalink
handle sse close connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Adminbck committed Jun 23, 2024
1 parent 2fe4482 commit f9186a6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ <h1>Server-Sent Events</h1>
<script>
const eventSource = new EventSource('http://melendriu.homepc.it:8888/events');
eventSource.onmessage = function(event) {
const dataElement = document.getElementById('sse-data');
dataElement.innerHTML = event.data + '<br>';
};
eventSource.onerror = function(event) {
dataElement.innerHTML = 'Finished.. <br>';
const dataElement = document.getElementById('sse-data');
if (event.data == "close") {
dataElement.innerHTML = 'Finished..<br>';
eventSource.close();
} else {
dataElement.innerHTML = event.data + '<br>';
}
};
</script>

Expand Down

0 comments on commit f9186a6

Please sign in to comment.