feat: progress bar, more realiable reconnects

This commit is contained in:
Felipe M 2025-05-13 15:11:35 +02:00
parent bcc1bce743
commit 7e8f6786fe
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
5 changed files with 305 additions and 30 deletions

View file

@ -149,10 +149,11 @@ func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request) {
// StatusResponse contains the jukebox status
type StatusResponse struct {
IsPlaying bool `json:"isPlaying"`
CurrentSong *subsonic.Song `json:"currentSong"`
SongHistory []*subsonic.Song `json:"songHistory"`
UpdateTime string `json:"updateTime"`
IsPlaying bool `json:"isPlaying"`
CurrentSong *subsonic.Song `json:"currentSong"`
CurrentSongStarted string `json:"currentSongStarted"`
SongHistory []*subsonic.Song `json:"songHistory"`
UpdateTime string `json:"updateTime"`
}
// handleStatus returns the current jukebox status as JSON
@ -165,15 +166,22 @@ func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
// getStatus returns the current status
func (s *Server) getStatus() StatusResponse {
currentSong := s.jukebox.GetCurrentSong()
currentSong, songStartTime := s.jukebox.GetCurrentSongInfo()
songHistory := s.jukebox.GetSongHistory()
isPlaying := s.bot.IsPlaying()
// Format the start time if it's not zero
startTimeStr := ""
if !songStartTime.IsZero() {
startTimeStr = songStartTime.Format(time.RFC3339)
}
return StatusResponse{
IsPlaying: isPlaying,
CurrentSong: currentSong,
SongHistory: songHistory,
UpdateTime: time.Now().Format(time.RFC3339),
IsPlaying: isPlaying,
CurrentSong: currentSong,
CurrentSongStarted: startTimeStr,
SongHistory: songHistory,
UpdateTime: time.Now().Format(time.RFC3339),
}
}
@ -208,7 +216,8 @@ func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
notifyChan <- true
}()
// Send initial status update
// Send initial status update as soon as connection is established
// This ensures the client receives current data immediately without waiting for changes
status := s.getStatus()
data, err := json.Marshal(status)
if err == nil {
@ -287,6 +296,7 @@ func (s *Server) broadcastUpdates() {
// Keep track of the previous status to only send updates when there are changes
var prevIsPlaying bool
var prevSongID string
var prevHistoryHash string
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
@ -309,12 +319,23 @@ func (s *Server) broadcastUpdates() {
currentSongID = status.CurrentSong.ID
}
hasChanged := (prevIsPlaying != status.IsPlaying) || (prevSongID != currentSongID)
// Create a simple hash of the history to detect changes
historyHash := ""
for _, song := range status.SongHistory {
if song != nil {
historyHash += song.ID + ":"
}
}
hasChanged := (prevIsPlaying != status.IsPlaying) ||
(prevSongID != currentSongID) ||
(prevHistoryHash != historyHash)
if hasChanged {
// Update previous state
prevIsPlaying = status.IsPlaying
prevSongID = currentSongID
prevHistoryHash = historyHash
// Broadcast to all clients
data, err := json.Marshal(status)