ChronoTomato/internal/websocket/connect.go
Sebastian Mark 9b7dc19935 feat: introduce vars.go for websocket package
- update comments
- move `Done` variable to vars.go
- move `Connected()` method

🤖
2024-11-21 20:07:08 +01:00

37 lines
838 B
Go

package websocket
import (
"time"
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
// New websocket client
type Client ChronoTomato.GoTomatoClient
// Connects to websocket
func Connect(url string) Client {
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
return Client{Conn: conn, Server: url, LastErr: err}
}
// Disconnects from websocket
func (c Client) Disconnect() {
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
if c.Connected() {
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-Done:
case <-time.After(time.Second):
}
}
return
}
func (c Client) Connected() bool {
return c.Conn != nil
}