ChronoTomato/internal/websocket/receive.go
Sebastian Mark 6857615585 fix: no error on normal websocket closure
- ignore error for normal closure
- log any other errors encountered during message processing
- reorder import statements

🤖
2024-10-24 07:25:48 +02:00

41 lines
834 B
Go

package websocket
import (
"encoding/json"
"fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
)
var Done = make(chan struct{})
func ProcessServerMessages(conn *websocket.Conn) {
defer close(Done)
for {
_, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, 1000) {
// Ignore normal closure and exit gracefully
return
}
// Log any other errors
fmt.Println()
log.Error("Read error!", "reason", err)
return
}
err = json.Unmarshal(message, &shared.ServerMessage)
if err != nil {
log.Error("Error unmarshalling!", "reason", err)
continue
}
notifications.DesktopNotifications()
notifications.TerminalOutput()
}
}