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,8 +3,6 @@ package client
|
||||||
import (
|
import (
|
||||||
"atomicgo.dev/cursor"
|
"atomicgo.dev/cursor"
|
||||||
"flag"
|
"flag"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
||||||
|
@ -15,11 +13,7 @@ import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var interrupt = make(chan os.Signal, 1)
|
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
signal.Notify(interrupt, os.Interrupt)
|
|
||||||
|
|
||||||
cursor.Hide()
|
cursor.Hide()
|
||||||
defer cursor.Show()
|
defer cursor.Show()
|
||||||
|
|
||||||
|
@ -46,6 +40,10 @@ func Start() {
|
||||||
|
|
||||||
pomodoro := &shared.ServerMessage
|
pomodoro := &shared.ServerMessage
|
||||||
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
||||||
|
select {
|
||||||
|
case <-websocket.Done:
|
||||||
|
return true, nil
|
||||||
|
default:
|
||||||
switch key.String() {
|
switch key.String() {
|
||||||
case "space":
|
case "space":
|
||||||
if !pomodoro.Ongoing {
|
if !pomodoro.Ongoing {
|
||||||
|
@ -69,12 +67,12 @@ func Start() {
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
case "q":
|
case "q":
|
||||||
interrupt <- os.Interrupt
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
websocket.WaitForDisconnect(conn, interrupt)
|
websocket.Disconnect(conn)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package websocket
|
||||||
import (
|
import (
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,13 +17,12 @@ func Connect(url string) *websocket.Conn {
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func WaitForDisconnect(conn *websocket.Conn, interrupt chan os.Signal) {
|
func Disconnect(conn *websocket.Conn) {
|
||||||
for {
|
|
||||||
select {
|
select {
|
||||||
case <-Done:
|
case <-Done:
|
||||||
// session closed by remote
|
// session closed by remote
|
||||||
return
|
return
|
||||||
case <-interrupt:
|
default:
|
||||||
// Cleanly close the connection by sending a close message and then
|
// Cleanly close the connection by sending a close message and then
|
||||||
// waiting (with timeout) for the server to close the connection.
|
// waiting (with timeout) for the server to close the connection.
|
||||||
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||||
|
@ -34,5 +32,4 @@ func WaitForDisconnect(conn *websocket.Conn, interrupt chan os.Signal) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue