feat: refactor signal handling and update websocket disconnect handling
- remove os signal handling interrupt
- rename WaitForDisconnect to Disconnect function
- improve websocket disconnection logic
- exit keyboard listener on disconnect
🤖
This commit is contained in:
parent
deff8f3554
commit
e14492a01c
2 changed files with 40 additions and 45 deletions
|
@ -3,7 +3,6 @@ package websocket
|
|||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -18,21 +17,19 @@ func Connect(url string) *websocket.Conn {
|
|||
return conn
|
||||
}
|
||||
|
||||
func WaitForDisconnect(conn *websocket.Conn, interrupt chan os.Signal) {
|
||||
for {
|
||||
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:
|
||||
// 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
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue