Sebastian Mark
bbc9977f1c
- remove `shared` package and shared.Message - rename `notifications` package to `frontend` - introduce a channel send the received ServerMessages to the frontend handler(s) - move keyhandler to goroutine - simplify password handling in Start function - update import names when importing from GoTomoto
42 lines
829 B
Go
42 lines
829 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, 1000) {
|
|
// 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
|
|
channel <- serverMessage
|
|
}
|
|
|
|
}
|