Created
April 3, 2017 20:13
-
-
Save toddself/e5588e0ed921abe6c1771ad1e8a2bad7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks yoshua https://github.com/yoshuawuyts/knowledge/blob/master/browser/sse.md