40 lines
825 B
Go
40 lines
825 B
Go
package websocket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
"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
|
|
|
|
defer close(Done)
|
|
|
|
for {
|
|
_, message, err := 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
|
|
}
|
|
}
|