Compare commits
4 commits
f1071f33c6
...
b06fac60d5
Author | SHA1 | Date | |
---|---|---|---|
b06fac60d5 | |||
b374ee8aff | |||
0ca90b50a5 | |||
b943c9d6eb |
7 changed files with 37 additions and 26 deletions
|
@ -35,11 +35,12 @@ func Start() {
|
||||||
config = helper.ParseConfig(*configfile)
|
config = helper.ParseConfig(*configfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := websocket.Connect(config.URL)
|
client := websocket.Connect(config.URL)
|
||||||
|
client.Password = config.Password
|
||||||
|
|
||||||
channel := make(chan GoTomato.ServerMessage)
|
channel := make(chan GoTomato.ServerMessage)
|
||||||
go websocket.ProcessServerMessages(conn, channel)
|
go websocket.ProcessServerMessages(client, channel)
|
||||||
frontend.UpdateLoop(conn, config, channel)
|
frontend.UpdateLoop(client, config, channel)
|
||||||
|
|
||||||
websocket.Disconnect(conn)
|
websocket.Disconnect(client)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package frontend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eiannone/keyboard"
|
"github.com/eiannone/keyboard"
|
||||||
ws "github.com/gorilla/websocket"
|
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||||
|
|
||||||
|
@ -21,16 +20,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyhandler(key keyboard.KeyEvent, conn *ws.Conn, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
|
func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
|
||||||
switch key.Rune {
|
switch key.Rune {
|
||||||
case 0: // space
|
case 0: // space
|
||||||
cmd := start_pause_resume(message)
|
cmd := start_pause_resume(message)
|
||||||
websocket.SendCmd(conn, config.Password, cmd)
|
client.SendCmd(cmd)
|
||||||
case 115: // s
|
case 115: // s
|
||||||
websocket.SendCmd(conn, config.Password, "stop")
|
client.SendCmd("stop")
|
||||||
case 114: // r
|
case 114: // r
|
||||||
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
|
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
|
||||||
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
|
client.SendSettingsUpdate(config.PomodoroConfig)
|
||||||
}
|
}
|
||||||
case 113: // q
|
case 113: // q
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -2,7 +2,6 @@ package frontend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eiannone/keyboard"
|
"github.com/eiannone/keyboard"
|
||||||
ws "github.com/gorilla/websocket"
|
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||||
|
|
||||||
|
@ -10,7 +9,7 @@ import (
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
|
func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
|
||||||
var message GoTomato.ServerMessage
|
var message GoTomato.ServerMessage
|
||||||
|
|
||||||
keysEvents, _ := keyboard.GetKeys(1)
|
keysEvents, _ := keyboard.GetKeys(1)
|
||||||
|
@ -22,7 +21,7 @@ func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoToma
|
||||||
desktopNotifications(message)
|
desktopNotifications(message)
|
||||||
terminalOutput(message)
|
terminalOutput(message)
|
||||||
case keypress := <-keysEvents:
|
case keypress := <-keysEvents:
|
||||||
if !keyhandler(keypress, conn, config, message) {
|
if !keyhandler(keypress, client, config, message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-websocket.Done:
|
case <-websocket.Done:
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Connect(url string) *websocket.Conn {
|
func Connect(url string) Client {
|
||||||
log.Info("Connected ó°–Ÿ ", "host", url)
|
log.Info("Connected ó°–Ÿ ", "host", url)
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||||
|
@ -14,10 +14,10 @@ func Connect(url string) *websocket.Conn {
|
||||||
log.Fatal("Dial error!", "reason", err)
|
log.Fatal("Dial error!", "reason", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn
|
return Client{Conn: conn}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Disconnect(conn *websocket.Conn) {
|
func Disconnect(client Client) {
|
||||||
select {
|
select {
|
||||||
case <-Done:
|
case <-Done:
|
||||||
// session closed by remote
|
// session closed by remote
|
||||||
|
@ -25,7 +25,7 @@ func Disconnect(conn *websocket.Conn) {
|
||||||
default:
|
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, ""))
|
client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||||
select {
|
select {
|
||||||
case <-Done:
|
case <-Done:
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
|
|
|
@ -11,13 +11,13 @@ import (
|
||||||
|
|
||||||
var Done = make(chan struct{})
|
var Done = make(chan struct{})
|
||||||
|
|
||||||
func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) {
|
func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) {
|
||||||
var serverMessage GoTomato.ServerMessage
|
var serverMessage GoTomato.ServerMessage
|
||||||
|
|
||||||
defer close(Done)
|
defer close(Done)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, message, err := conn.ReadMessage()
|
_, message, err := client.Conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||||
// Ignore normal closure and exit gracefully
|
// Ignore normal closure and exit gracefully
|
||||||
|
|
|
@ -5,38 +5,41 @@ import (
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
|
type Client ChronoTomato.GoTomatoClient // New websocket client
|
||||||
|
|
||||||
|
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
|
||||||
messageBytes, err := json.Marshal(msg)
|
messageBytes, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error marshalling!", "reason", err)
|
log.Error("Error marshalling!", "reason", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.WriteMessage(websocket.TextMessage, messageBytes)
|
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Write error!", "reason", err)
|
log.Error("Write error!", "reason", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
func (c Client) SendCmd(cmd string) {
|
||||||
message := GoTomato.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: cmd,
|
Command: cmd,
|
||||||
Password: pwd,
|
Password: c.Password,
|
||||||
}
|
}
|
||||||
|
|
||||||
sendClientCommand(conn, message)
|
c.sendClientCommand(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.PomodoroConfig) {
|
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||||
message := GoTomato.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: "updateSettings",
|
Command: "updateSettings",
|
||||||
Password: pwd,
|
Password: c.Password,
|
||||||
Settings: settings,
|
Settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
sendClientCommand(conn, message)
|
c.sendClientCommand(message)
|
||||||
}
|
}
|
||||||
|
|
9
pkg/models/client.go
Normal file
9
pkg/models/client.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in a new issue