Sebastian Mark
561419b568
- remove direct log usage
- create a new logging helper
- update all log calls to use the new Logger instance
- set timestamp to ISO8601
🤖
41 lines
957 B
Go
41 lines
957 B
Go
package websocket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gorilla/websocket"
|
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
|
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
|
|
helper.Logger.Error("Read error!", "reason", err)
|
|
close(channel)
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(message, &serverMessage)
|
|
if err != nil {
|
|
helper.Logger.Error("Error unmarshalling!", "reason", err)
|
|
continue
|
|
}
|
|
|
|
channel <- serverMessage
|
|
}
|
|
}
|