Created
January 6, 2020 15:35
-
-
Save colinbes/bf524170885380891620dcf368923c33 to your computer and use it in GitHub Desktop.
Vuejs file for EventSource
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 app = new Vue({ | |
el: '#app', | |
data: { | |
message: 'Vue/SSE Example!', | |
now: 'wait for it ...' | |
}, | |
created () { | |
this.setupStream() | |
}, | |
methods: { | |
setup () { | |
this.now = 'yup' | |
}, | |
setupStream () { | |
let evtSource = new EventSource("http://localhost:8082/events") | |
evtSource.addEventListener('myEvent', event => { | |
let data = JSON.parse(event.data) | |
this.now = data.msg | |
}, false) | |
evtSource.addEventListener('error', event => { | |
if (event.readyState == EventSource.CLOSED) { | |
console.log('Event was closed'); | |
console.log(EventSource); | |
} | |
}, false); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment