2024-10-22 11:13:41 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-10-24 05:25:48 +00:00
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
|
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
2024-10-22 11:13:41 +00:00
|
|
|
"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 {
|
2024-10-24 05:25:48 +00:00
|
|
|
if websocket.IsCloseError(err, 1000) {
|
|
|
|
// Ignore normal closure and exit gracefully
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Log any other errors
|
2024-10-22 11:13:41 +00:00
|
|
|
fmt.Println()
|
|
|
|
log.Error("Read error!", "reason", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-23 11:16:30 +00:00
|
|
|
err = json.Unmarshal(message, &shared.ServerMessage)
|
2024-10-22 11:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error unmarshalling!", "reason", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-23 11:16:30 +00:00
|
|
|
notifications.DesktopNotifications()
|
|
|
|
notifications.TerminalOutput()
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|