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

View File

@ -9,6 +9,7 @@ import Analytics from "./Analytics"
import SideBar from "./SideBar"
import InfiniteScroller from "./InfiniteScroller"
import ServiceWorkerManager from "./ServiceWorkerManager"
import ServerEvents from "./ServerEvents"
import { checkNewVersionDelayed } from "./NewVersionCheck"
import { displayAiringDate, displayDate, displayTime } from "./DateView"
import { findAll, canUseWebP, requestIdleCallback, swapElements, delay, findAllInside } from "./Utils"
@ -35,6 +36,7 @@ export default class AnimeNotifier {
diffCompletedForCurrentPath: boolean
lastReloadContentPath: string
currentSoundTrackId: string
serverEvents: ServerEvents
constructor(app: Application) {
this.app = app
@ -235,6 +237,11 @@ export default class AnimeNotifier {
window.resizeTo(finalWidth, finalHeight)
}
// Server sent events
if(this.user && EventSource) {
this.serverEvents = new ServerEvents()
}
// // Download popular anime titles for the search
// let response = await fetch("/api/popular/anime/titles/500")
// let titles = await response.json()

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")
}
}