2024-10-22 11:13:41 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
"github.com/gorilla/websocket"
|
2024-10-27 20:34:53 +00:00
|
|
|
|
|
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
2024-10-22 11:13:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Done = make(chan struct{})
|
|
|
|
|
2024-10-30 10:03:18 +00:00
|
|
|
// Receives websocket messages and write them to `channel`
|
2024-10-30 08:51:59 +00:00
|
|
|
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
2024-10-27 09:41:31 +00:00
|
|
|
var serverMessage GoTomato.ServerMessage
|
2024-10-22 11:13:41 +00:00
|
|
|
|
|
|
|
defer close(Done)
|
|
|
|
|
|
|
|
for {
|
2024-10-30 08:51:59 +00:00
|
|
|
_, message, err := c.Conn.ReadMessage()
|
2024-10-22 11:13:41 +00:00
|
|
|
if err != nil {
|
2024-10-27 15:46:11 +00:00
|
|
|
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
2024-10-24 05:25:48 +00:00
|
|
|
// 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-27 09:41:31 +00:00
|
|
|
err = json.Unmarshal(message, &serverMessage)
|
2024-10-22 11:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error unmarshalling!", "reason", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-27 09:41:31 +00:00
|
|
|
channel <- serverMessage
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
}
|