|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<title>Server-Sent Events Demo</title> |
|
<meta charset="UTF-8" /> |
|
<script> |
|
window.addEventListener("load", function() { |
|
var button = document.getElementById("connect"); |
|
var status = document.getElementById("status"); |
|
var output = document.getElementById("output"); |
|
var connectTime = document.getElementById("connecttime"); |
|
var source; |
|
|
|
function connect() { |
|
source = new EventSource("stream"); |
|
source.addEventListener("message", function(event) { |
|
output.textContent = event.data; |
|
}, false); |
|
|
|
source.addEventListener("connecttime", function(event) { |
|
connectTime.textContent = "Connection was last established at: " + event.data; |
|
}, false); |
|
|
|
source.addEventListener("open", function(event) { |
|
button.value = "Disconnect"; |
|
button.onclick = function(event) { |
|
source.close(); |
|
button.value = "Connect"; |
|
button.onclick = connect; |
|
status.textContent = "Connection closed!"; |
|
}; |
|
status.textContent = "Connection open!"; |
|
}, false); |
|
|
|
source.addEventListener("error", function(event) { |
|
if (event.target.readyState === EventSource.CLOSED) { |
|
source.close(); |
|
status.textContent = "Connection closed!"; |
|
} else if (event.target.readyState === EventSource.CONNECTING) { |
|
status.textContent = "Connection closed. Attempting to reconnect!"; |
|
} else { |
|
status.textContent = "Connection closed. Unknown error!"; |
|
} |
|
}, false); |
|
} |
|
|
|
if (!!window.EventSource) { |
|
connect(); |
|
} else { |
|
button.style.display = "none"; |
|
status.textContent = "Sorry, your browser doesn't support server-sent events"; |
|
} |
|
}, false); |
|
</script> |
|
</head> |
|
<body> |
|
<input type="button" id="connect" value="Connect" /><br /> |
|
<span id="status">Connection closed!</span><br /> |
|
<span id="connecttime"></span><br /> |
|
<span id="output"></span> |
|
</body> |
|
</html> |