Skip to content

Instantly share code, notes, and snippets.

@toddself
Created April 3, 2017 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddself/e5588e0ed921abe6c1771ad1e8a2bad7 to your computer and use it in GitHub Desktop.
Save toddself/e5588e0ed921abe6c1771ad1e8a2bad7 to your computer and use it in GitHub Desktop.
var source = new EventSource('stream')
source.addEventListener('message', function (event) {
console.log('new message', event.data)
}, false)
// this is our custom event
source.addEventListener('connecttime', function (event) {
console.log('Connection was last established at: ' + event.data)
}, false)
source.addEventListener('open', function (event) {
document.body.addEventListener('click', function () {
source.close()
console.log('connection closed')
})
}, false)
source.addEventListener('error', function (event) {
if (event.target.readyState === EventSource.CLOSED) {
source.close()
console.log('Connection closed!')
} else if (event.target.readyState === EventSource.CONNECTING) {
console.log('Connection closed. Attempting to reconnect!')
} else {
console.log('Connection closed. Unknown error!')
}
}, false)
@toddself
Copy link
Author

toddself commented Apr 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment