ChronoTomato/internal/websocket/receive.go

42 lines
901 B
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"fmt"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
var Done = make(chan struct{})
func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
2024-10-22 11:13:41 +00:00
defer close(Done)
for {
_, message, err := client.Conn.ReadMessage()
2024-10-22 11:13:41 +00:00
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// 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
}
err = json.Unmarshal(message, &serverMessage)
2024-10-22 11:13:41 +00:00
if err != nil {
log.Error("Error unmarshalling!", "reason", err)
continue
}
channel <- serverMessage
2024-10-22 11:13:41 +00:00
}
}