ChronoTomato/internal/websocket/receive.go

43 lines
829 B
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"fmt"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
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, channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
2024-10-22 11:13:41 +00:00
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
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
channel <- serverMessage
2024-10-22 11:13:41 +00:00
}
}