ChronoTomato/internal/websocket/receive.go
Sebastian Mark b943c9d6eb feat: refactor connection handling to use models.Client
- introduce `Client` model
- modify all websocket operations to use the new `Client` model
- update function signatures to accept `models.Client` instead of `*websocket.Conn`
- ensure consistent usage of `client` across all related functions

🤖
2024-10-30 08:28:22 +01:00

41 lines
901 B
Go

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"
)
var Done = make(chan struct{})
func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
defer close(Done)
for {
_, message, err := client.Conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully
return
}
// Log any other errors
fmt.Println()
log.Error("Read error!", "reason", err)
return
}
err = json.Unmarshal(message, &serverMessage)
if err != nil {
log.Error("Error unmarshalling!", "reason", err)
continue
}
channel <- serverMessage
}
}