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 07:54:58 +00:00
|
|
|
func ProcessServerMessages(client Client, 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 07:28:22 +00:00
|
|
|
_, message, err := client.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
|
|
|
}
|
|
|
|
}
|