Last active
March 17, 2019 23:57
-
-
Save karenpeng/5db5763aa3fa66e65c28ac8dd93dcecb 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
const evtSource = new EventSource('/sse'); | |
evtSource.onmessage = event => { | |
try { | |
const jsonData = JSON.parse(event.data); | |
console.log(jsonData); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
evtSource.onerror = event => { | |
if (event.readyState === EventSource.CLOSED) { | |
console.log('connection is closed') | |
} | |
} | |
evtSource.onopen = event => { | |
console.log('connection is opened') | |
} | |
setTimeout(() => { | |
console.log('client closes the connection'); | |
evtSource.close(); | |
}, 30000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment