From 369b658d60dc560dcc705f4e55b9ab5a6e082038 Mon Sep 17 00:00:00 2001 From: nite07 Date: Mon, 28 Oct 2024 15:52:02 +0800 Subject: [PATCH] fix: front-end not save token fix: sometimes not auto stream next video --- server/static/index.html | 17 ++++++++++++----- streamer/streamer.go | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/server/static/index.html b/server/static/index.html index 7b6b98f..54ef269 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -378,7 +378,7 @@ ws.onopen = function () { console.log("Connected to WebSocket"); - setStoredToken(document.getElementById("token-input").value); + setStoredToken(token); document.getElementById("token-screen").style.display = "none"; document.querySelector(".container-fluid").style.display = "flex"; document.getElementById("status").textContent = @@ -404,7 +404,6 @@ }; ws.onerror = function () { - localStorage.removeItem("streaming_token"); document.getElementById("token-error").style.display = "block"; }; @@ -425,12 +424,20 @@ localStorage.setItem("streaming_token", token); } + document.addEventListener("DOMContentLoaded", function () { + const storedToken = getStoredToken(); + if (storedToken) { + document.getElementById("token-input").value = storedToken; + validateToken(); + } + }); + function validateToken() { const tokenInput = document.getElementById("token-input"); - const token = tokenInput.value || getStoredToken(); - if (token) { - tokenInput.value = token; + if (tokenInput.value) { connectWebSocket(); + } else { + document.getElementById("token-error").style.display = "block"; } } diff --git a/streamer/streamer.go b/streamer/streamer.go index f0d100c..51fce14 100644 --- a/streamer/streamer.go +++ b/streamer/streamer.go @@ -129,8 +129,14 @@ func (s *Streamer) handleStart() { s.writeOutput(fmt.Sprintf("stop stream: %s\n", videoPath)) if !s.state.manualControl { - s.mailbox <- NextVideoMessage{} + log.Println("ready to stream next video") + s.state.currentVideoIndex++ + if s.state.currentVideoIndex >= len(s.state.videoList) { + s.state.currentVideoIndex = 0 + } + s.mailbox <- StartMessage{} } else { + log.Println("manually control") s.state.manualControl = false } @@ -143,15 +149,22 @@ func (s *Streamer) handleStop() { return } + log.Println("wait context to be cancelled") s.state.cancel() + log.Println("context has been cancelled") if s.state.cmd.Process != nil { + log.Println("wait ffmpeg process stop") select { case <-s.state.waitDone: case <-time.After(3 * time.Second): _ = s.state.cmd.Process.Kill() } + log.Println("ffmpeg process has stopped") } + + s.state.cancel = nil + s.state.cmd = nil } func (s *Streamer) handleAdd(path string) {