From e14492a01cefe1ad975ff4ee0ed737db03a56762 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 23 Oct 2024 19:04:52 +0200 Subject: [PATCH] feat: refactor signal handling and update websocket disconnect handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove os signal handling interrupt - rename WaitForDisconnect to Disconnect function - improve websocket disconnection logic - exit keyboard listener on disconnect 🤖 --- cmd/client/main.go | 60 +++++++++++++++++------------------ internal/websocket/connect.go | 25 +++++++-------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 458c035..11243f1 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -3,8 +3,6 @@ package client import ( "atomicgo.dev/cursor" "flag" - "os" - "os/signal" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/shared" @@ -15,11 +13,7 @@ import ( "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -var interrupt = make(chan os.Signal, 1) - func Start() { - signal.Notify(interrupt, os.Interrupt) - cursor.Hide() defer cursor.Show() @@ -46,35 +40,39 @@ func Start() { pomodoro := &shared.ServerMessage keyboard.Listen(func(key keys.Key) (stop bool, err error) { - switch key.String() { - case "space": - if !pomodoro.Ongoing { - websocket.SendCmd(conn, password, "start") - return false, nil - } - if pomodoro.Paused { - websocket.SendCmd(conn, password, "resume") - return false, nil - } else { - websocket.SendCmd(conn, password, "pause") - return false, nil - } - case "s": - websocket.SendCmd(conn, password, "stop") - return false, nil - case "r": - - if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) { - websocket.Send_updateSettings(conn, password, config.PomodoroConfig) - } - return false, nil - case "q": - interrupt <- os.Interrupt + select { + case <-websocket.Done: return true, nil + default: + switch key.String() { + case "space": + if !pomodoro.Ongoing { + websocket.SendCmd(conn, password, "start") + return false, nil + } + if pomodoro.Paused { + websocket.SendCmd(conn, password, "resume") + return false, nil + } else { + websocket.SendCmd(conn, password, "pause") + return false, nil + } + case "s": + websocket.SendCmd(conn, password, "stop") + return false, nil + case "r": + + if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) { + websocket.Send_updateSettings(conn, password, config.PomodoroConfig) + } + return false, nil + case "q": + return true, nil + } } return false, nil }) - websocket.WaitForDisconnect(conn, interrupt) + websocket.Disconnect(conn) } diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index de7f5ef..fd3c7ee 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -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 } }