Sebastian Mark
8a0ef32c91
- implement TUI in bubbletea - split components into separate files - remove now unused functions - restructure files
42 lines
968 B
Go
42 lines
968 B
Go
package websocket
|
|
|
|
import (
|
|
"github.com/charmbracelet/log"
|
|
"github.com/gorilla/websocket"
|
|
"time"
|
|
|
|
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 {
|
|
log.Fatal("Dial error!", "reason", err)
|
|
}
|
|
|
|
log.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
|
|
}
|
|
}
|