Genesis
This commit is contained in:
commit
11c599a371
11 changed files with 325 additions and 0 deletions
38
internal/websocket/connect.go
Normal file
38
internal/websocket/connect.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"os"
|
||||
"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 WaitForDisconnect(conn *websocket.Conn, interrupt chan os.Signal) {
|
||||
for {
|
||||
select {
|
||||
case <-Done:
|
||||
// session closed by remote
|
||||
return
|
||||
case <-interrupt:
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue