Sebastian Mark
e14492a01c
- remove os signal handling interrupt
- rename WaitForDisconnect to Disconnect function
- improve websocket disconnection logic
- exit keyboard listener on disconnect
🤖
35 lines
761 B
Go
35 lines
761 B
Go
package websocket
|
|
|
|
import (
|
|
"github.com/charmbracelet/log"
|
|
"github.com/gorilla/websocket"
|
|
"time"
|
|
)
|
|
|
|
func Connect(url string) *websocket.Conn {
|
|
log.Info("Connected ó°–Ÿ ", "host", url)
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
|
if err != nil {
|
|
log.Fatal("Dial error!", "reason", err)
|
|
}
|
|
|
|
return conn
|
|
}
|
|
|
|
func Disconnect(conn *websocket.Conn) {
|
|
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.
|
|
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
|
select {
|
|
case <-Done:
|
|
case <-time.After(time.Second):
|
|
}
|
|
return
|
|
}
|
|
}
|