feat: refactor client methods
- change `ProcessServerMessages` to use receiver instead of parameter
- update `Disconnect` method to use receiver for better encapsulation
- move `Client` definition to `connect.go`
🤖
This commit is contained in:
parent
480f1c0cdd
commit
6187122b81
4 changed files with 10 additions and 9 deletions
|
@ -49,8 +49,8 @@ func Start() {
|
|||
client.Password = config.Password
|
||||
|
||||
channel := make(chan GoTomato.ServerMessage)
|
||||
go websocket.ProcessServerMessages(client, channel)
|
||||
go client.ProcessServerMessages(channel)
|
||||
frontend.UpdateLoop(client, config, channel)
|
||||
|
||||
websocket.Disconnect(client)
|
||||
client.Disconnect()
|
||||
}
|
||||
|
|
|
@ -4,8 +4,12 @@ import (
|
|||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
type Client ChronoTomato.GoTomatoClient // New websocket client
|
||||
|
||||
func Connect(url string) Client {
|
||||
log.Info("Connected ", "host", url)
|
||||
|
||||
|
@ -17,7 +21,7 @@ func Connect(url string) Client {
|
|||
return Client{Conn: conn}
|
||||
}
|
||||
|
||||
func Disconnect(client Client) {
|
||||
func (c Client) Disconnect() {
|
||||
select {
|
||||
case <-Done:
|
||||
// session closed by remote
|
||||
|
@ -25,7 +29,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, ""))
|
||||
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
select {
|
||||
case <-Done:
|
||||
case <-time.After(time.Second):
|
||||
|
|
|
@ -11,13 +11,13 @@ import (
|
|||
|
||||
var Done = make(chan struct{})
|
||||
|
||||
func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) {
|
||||
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage GoTomato.ServerMessage
|
||||
|
||||
defer close(Done)
|
||||
|
||||
for {
|
||||
_, message, err := client.Conn.ReadMessage()
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
// Ignore normal closure and exit gracefully
|
||||
|
|
|
@ -5,12 +5,9 @@ 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) {
|
||||
messageBytes, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue