Sebastian Mark
05e8bf5854
- add `Connected()` method for client - add `LastErr` field to GoTomatoClient - replace error logging by updating `LastErr` field - modify methods to use pointer receivers for Client - add `prevMessage` variable to store the last received server message - show connect status in TUI - use EnterAltScreen instead of ClearScreen
35 lines
837 B
Go
35 lines
837 B
Go
package websocket
|
|
|
|
import (
|
|
"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)
|
|
|
|
return Client{Conn: conn, Server: url, LastErr: err}
|
|
}
|
|
|
|
func (c Client) Connected() bool {
|
|
return c.Conn != nil
|
|
}
|
|
|
|
// 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
|
|
}
|