Compare commits

..

No commits in common. "b06fac60d547805d90bbf235f7c85d7df7089bb0" and "f1071f33c6a14e9e78aa6d4a740212810b987cf1" have entirely different histories.

7 changed files with 26 additions and 37 deletions

View file

@ -35,12 +35,11 @@ func Start() {
config = helper.ParseConfig(*configfile)
}
client := websocket.Connect(config.URL)
client.Password = config.Password
conn := websocket.Connect(config.URL)
channel := make(chan GoTomato.ServerMessage)
go websocket.ProcessServerMessages(client, channel)
frontend.UpdateLoop(client, config, channel)
go websocket.ProcessServerMessages(conn, channel)
frontend.UpdateLoop(conn, config, channel)
websocket.Disconnect(client)
websocket.Disconnect(conn)
}

View file

@ -2,6 +2,7 @@ package frontend
import (
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -20,16 +21,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string {
}
}
func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
func keyhandler(key keyboard.KeyEvent, conn *ws.Conn, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
switch key.Rune {
case 0: // space
cmd := start_pause_resume(message)
client.SendCmd(cmd)
websocket.SendCmd(conn, config.Password, cmd)
case 115: // s
client.SendCmd("stop")
websocket.SendCmd(conn, config.Password, "stop")
case 114: // r
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
client.SendSettingsUpdate(config.PomodoroConfig)
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
case 113: // q
return false

View file

@ -2,6 +2,7 @@ package frontend
import (
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -9,7 +10,7 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message GoTomato.ServerMessage
keysEvents, _ := keyboard.GetKeys(1)
@ -21,7 +22,7 @@ func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-c
desktopNotifications(message)
terminalOutput(message)
case keypress := <-keysEvents:
if !keyhandler(keypress, client, config, message) {
if !keyhandler(keypress, conn, config, message) {
return
}
case <-websocket.Done:

View file

@ -6,7 +6,7 @@ import (
"time"
)
func Connect(url string) Client {
func Connect(url string) *websocket.Conn {
log.Info("Connected ó°–ź ", "host", url)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
@ -14,10 +14,10 @@ func Connect(url string) Client {
log.Fatal("Dial error!", "reason", err)
}
return Client{Conn: conn}
return conn
}
func Disconnect(client Client) {
func Disconnect(conn *websocket.Conn) {
select {
case <-Done:
// session closed by remote
@ -25,7 +25,7 @@ func Disconnect(client Client) {
default:
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-Done:
case <-time.After(time.Second):

View file

@ -11,13 +11,13 @@ import (
var Done = make(chan struct{})
func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) {
func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
defer close(Done)
for {
_, message, err := client.Conn.ReadMessage()
_, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully

View file

@ -5,41 +5,38 @@ import (
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
type Client ChronoTomato.GoTomatoClient // New websocket client
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
}
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
err = conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil {
log.Error("Write error!", "reason", err)
return
}
}
func (c Client) SendCmd(cmd string) {
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
message := GoTomato.ClientCommand{
Command: cmd,
Password: c.Password,
Password: pwd,
}
c.sendClientCommand(message)
sendClientCommand(conn, message)
}
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: c.Password,
Password: pwd,
Settings: settings,
}
c.sendClientCommand(message)
sendClientCommand(conn, message)
}

View file

@ -1,9 +0,0 @@
package models
import "github.com/gorilla/websocket"
// Represents a websocket client
type GoTomatoClient struct {
Conn *websocket.Conn // The websocket connection of the client
Password string // Pomodoro password
}