ChronoTomato/internal/websocket/connect.go
Sebastian Mark 561419b568 feat: implement centralized logging helper
- remove direct log usage
- create a new logging helper
- update all log calls to use the new Logger instance
- set timestamp to ISO8601

🤖
2024-11-04 20:39:21 +01:00

42 lines
1,011 B
Go

package websocket
import (
"github.com/gorilla/websocket"
"time"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
type Client ChronoTomato.GoTomatoClient // New websocket client
// Connects to websocket
func Connect(url string) Client {
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
helper.Logger.Fatal("Dial error!", "reason", err)
}
helper.Logger.Info("Connected ó°–Ÿ ", "host", url)
time.Sleep(time.Second)
return Client{Conn: conn}
}
// Disconnects from websocket
func (c Client) Disconnect() {
select {
case <-Done:
// session closed by remote
return
default:
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-Done:
case <-time.After(time.Second):
}
return
}
}