Implemented server sent events

This commit is contained in:
2018-11-07 05:40:03 +09:00
parent 61454b3e5b
commit e56782d5a3
6 changed files with 81 additions and 1 deletions

26
scripts/ServerEvents.ts Normal file
View File

@ -0,0 +1,26 @@
class ServerEvent {
data: string
}
export default class ServerEvents {
supported: boolean
eventSource: EventSource
constructor() {
this.supported = ("EventSource" in window)
if(!this.supported) {
return
}
this.eventSource = new EventSource("/api/sse/events", {
withCredentials: true
})
this.eventSource.addEventListener("ping", (e: any) => this.ping(e))
}
ping(e: ServerEvent) {
console.log("sse: ping")
}
}