mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 20:50:24 +00:00
Add EventSource example
Port from extend/cowboy_examples.
This commit is contained in:
parent
e3daf439da
commit
a302fe5007
10 changed files with 187 additions and 0 deletions
44
examples/eventsource/priv/index.html
Normal file
44
examples/eventsource/priv/index.html
Normal file
|
@ -0,0 +1,44 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script type="text/javascript">
|
||||
function ready() {
|
||||
if (!!window.EventSource) {
|
||||
setupEventSource();
|
||||
} else {
|
||||
document.getElementById('status').innerHTML =
|
||||
"Sorry but your browser doesn't support the EventSource API";
|
||||
}
|
||||
}
|
||||
|
||||
function setupEventSource() {
|
||||
var source = new EventSource('/eventsource');
|
||||
|
||||
source.addEventListener('message', function(event) {
|
||||
addStatus("server sent the following: '" + event.data + "'");
|
||||
}, false);
|
||||
|
||||
source.addEventListener('open', function(event) {
|
||||
addStatus('eventsource connected.')
|
||||
}, false);
|
||||
|
||||
source.addEventListener('error', function(event) {
|
||||
if (event.eventPhase == EventSource.CLOSED) {
|
||||
addStatus('eventsource was closed.')
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
|
||||
function addStatus(text) {
|
||||
var date = new Date();
|
||||
document.getElementById('status').innerHTML
|
||||
= document.getElementById('status').innerHTML
|
||||
+ date + ": " + text + "<br/>";
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="ready();">
|
||||
Hi!
|
||||
<div id="status"></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue