Sebastian Mark
b943c9d6eb
- introduce `Client` model
- modify all websocket operations to use the new `Client` model
- update function signatures to accept `models.Client` instead of `*websocket.Conn`
- ensure consistent usage of `client` across all related functions
🤖
37 lines
838 B
Go
37 lines
838 B
Go
package websocket
|
|
|
|
import (
|
|
"github.com/charmbracelet/log"
|
|
"github.com/gorilla/websocket"
|
|
"time"
|
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
|
)
|
|
|
|
func Connect(url string) models.Client {
|
|
log.Info("Connected ó°–Ÿ ", "host", url)
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
|
if err != nil {
|
|
log.Fatal("Dial error!", "reason", err)
|
|
}
|
|
|
|
return models.Client{Conn: conn}
|
|
}
|
|
|
|
func Disconnect(client models.Client) {
|
|
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.
|
|
client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
|
select {
|
|
case <-Done:
|
|
case <-time.After(time.Second):
|
|
}
|
|
return
|
|
}
|
|
}
|