diff options
author | metamuffin <metamuffin@disroot.org> | 2024-01-21 15:17:05 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-01-21 15:17:05 +0100 |
commit | 06d5c0d961c85abb3dd645b65b4447936fe7690f (patch) | |
tree | 83684a1ccdaf078541836e25f5992e9a9754ae32 /web/script/player/sync.ts | |
parent | 86582075d1bc4eacd664b5eb48c4472e1f2119b0 (diff) | |
download | jellything-06d5c0d961c85abb3dd645b65b4447936fe7690f.tar jellything-06d5c0d961c85abb3dd645b65b4447936fe7690f.tar.bz2 jellything-06d5c0d961c85abb3dd645b65b4447936fe7690f.tar.zst |
refactor streamsync a bit
Diffstat (limited to 'web/script/player/sync.ts')
-rw-r--r-- | web/script/player/sync.ts | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/web/script/player/sync.ts b/web/script/player/sync.ts index a2029ea..6f2b86f 100644 --- a/web/script/player/sync.ts +++ b/web/script/player/sync.ts @@ -16,17 +16,22 @@ export class Playersync { private ws: WebSocket private on_destroy: (() => void)[] = [] + public name: string + private cancel_pers: undefined | (() => void) set_pers(s?: string) { if (this.cancel_pers) this.cancel_pers(), this.cancel_pers = undefined if (s) this.cancel_pers = this.logger?.log_persistent(s) } - constructor(private player: Player, private logger: Logger<string>, private channel_name: string) { + constructor(private player: Player, private logger: Logger<string>, private channel_name?: string) { this.set_pers("Playersync enabling...") - let [localpart, remotepart] = channel_name.split(":") + channel_name ??= Math.random().toString(16).padEnd(5, "0").substring(2).substring(0, 6) + let [localpart, remotepart, port] = channel_name.split(":") if (!remotepart?.length) remotepart = window.location.host + if (port) remotepart += ":" + port + this.name = localpart + ":" + remotepart this.ws = new WebSocket(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${remotepart}/playersync/${encodeURIComponent(localpart)}`) this.on_destroy.push(() => this.ws.close()) @@ -54,25 +59,31 @@ export class Playersync { if (packet.playing === true) this.player.play() if (packet.playing === false) this.player.pause() if (packet.join) this.logger.log(`${packet.join} joined.`) - if (packet.leave) this.logger.log(`${packet.join} left.`) + if (packet.leave) this.logger.log(`${packet.leave} left.`) } let cb: () => void + const send_time = () => { + const time = this.player.video.currentTime + if (Math.abs(last_time - time) < 0.01) return + this.send({ time: this.player.video.currentTime }) + } + player.video.addEventListener("play", cb = () => { + send_time() this.send({ playing: true }) }) this.on_destroy.push(() => player.video.removeEventListener("play", cb)) player.video.addEventListener("pause", cb = () => { this.send({ playing: false }) + send_time() }) this.on_destroy.push(() => player.video.removeEventListener("pause", cb)) player.video.addEventListener("seeking", cb = () => { - const time = this.player.video.currentTime - if (Math.abs(last_time - time) < 0.01) return - this.send({ time: this.player.video.currentTime }) + send_time() }) this.on_destroy.push(() => player.video.removeEventListener("seeking", cb)) } @@ -81,7 +92,6 @@ export class Playersync { this.set_pers() this.logger.log("Playersync disabled.") this.on_destroy.forEach(f => f()) - this.send({ leave: get_username() }) } send(p: Packet) { |