feat: refactor connection handling to use models.Client
- 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
🤖
This commit is contained in:
parent
f1071f33c6
commit
b943c9d6eb
6 changed files with 26 additions and 24 deletions
|
@ -4,9 +4,11 @@ import (
|
|||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
func Connect(url string) *websocket.Conn {
|
||||
func Connect(url string) models.Client {
|
||||
log.Info("Connected ", "host", url)
|
||||
|
||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||
|
@ -14,10 +16,10 @@ func Connect(url string) *websocket.Conn {
|
|||
log.Fatal("Dial error!", "reason", err)
|
||||
}
|
||||
|
||||
return conn
|
||||
return models.Client{Conn: conn}
|
||||
}
|
||||
|
||||
func Disconnect(conn *websocket.Conn) {
|
||||
func Disconnect(client models.Client) {
|
||||
select {
|
||||
case <-Done:
|
||||
// session closed by remote
|
||||
|
@ -25,7 +27,7 @@ func Disconnect(conn *websocket.Conn) {
|
|||
default:
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) for the server to close the connection.
|
||||
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
select {
|
||||
case <-Done:
|
||||
case <-time.After(time.Second):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue