ChronoTomato/internal/websocket/connect.go
Sebastian Mark 0ca90b50a5 feat: refactor client handling and introduce websocket client type
- add new websocket.Client type
- change Connect and Disconnect functions to use the new Client type
- implement methods for sending commands and updating settings on Client
- update keyhandler to use websocket.Client instead of ChronoTomato.Client
- modify UpdateLoop to accept websocket.Client
- refactor ProcessServerMessages to accept the new Client type

🤖
2024-10-30 08:57:10 +01:00

35 lines
766 B
Go

package websocket
import (
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"time"
)
func Connect(url string) Client {
log.Info("Connected ó°–Ÿ ", "host", url)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
log.Fatal("Dial error!", "reason", err)
}
return Client{Conn: conn}
}
func Disconnect(client 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
}
}