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:
Sebastian Mark 2024-10-23 19:04:52 +02:00
parent deff8f3554
commit e14492a01c
2 changed files with 40 additions and 45 deletions

View file

@ -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,35 +40,39 @@ 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) {
switch key.String() { select {
case "space": case <-websocket.Done:
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
return true, nil 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 return false, nil
}) })
websocket.WaitForDisconnect(conn, interrupt) websocket.Disconnect(conn)
} }

View file

@ -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,21 +17,19 @@ 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 {
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 { select {
case <-Done: case <-Done:
// session closed by remote case <-time.After(time.Second):
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
} }
return
} }
} }