ChronoTomato/internal/websocket/receive.go
Sebastian Mark 8a0ef32c91 feat: use bubbletea framework
- implement TUI in bubbletea
  - split components into separate files
- remove now unused functions
- restructure files
2024-11-02 10:55:15 +01:00

41 lines
914 B
Go

package websocket
import (
"encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var Done = make(chan struct{})
// Receives websocket messages and writes them to a channel.
// Closes the channel if websocket closes.
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
defer close(Done)
for {
_, message, err := c.Conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully
return
}
// Log any other errors
log.Error("Read error!", "reason", err)
close(channel)
return
}
err = json.Unmarshal(message, &serverMessage)
if err != nil {
log.Error("Error unmarshalling!", "reason", err)
continue
}
channel <- serverMessage
}
}