diff --git a/cache/webchat.go b/cache/webchat.go index 3e423c1..601eb39 100644 --- a/cache/webchat.go +++ b/cache/webchat.go @@ -3,7 +3,6 @@ package cache import ( "encoding/json" "fmt" - "github.com/TicketsBot/GoPanel/app/http/endpoints/manage" ) type TicketMessage struct { @@ -13,7 +12,7 @@ type TicketMessage struct { Content string `json:"content"` } -func (c *RedisClient) ListenForMessages() { +func (c *RedisClient) ListenForMessages(message chan TicketMessage) { pubsub := c.Subscribe("tickets:webchat:inboundmessage") for { @@ -28,14 +27,6 @@ func (c *RedisClient) ListenForMessages() { continue } - manage.SocketsLock.Lock() - for _, socket := range manage.Sockets { - if socket.Guild == decoded.GuildId && socket.Ticket == decoded.TicketId { - if err := socket.Ws.WriteJSON(decoded); err != nil { - fmt.Println(err.Error()) - } - } - } - manage.SocketsLock.Unlock() + message<-decoded } } diff --git a/cmd/panel/main.go b/cmd/panel/main.go index b658713..4578464 100644 --- a/cmd/panel/main.go +++ b/cmd/panel/main.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "github.com/TicketsBot/GoPanel/app/http" + "github.com/TicketsBot/GoPanel/app/http/endpoints/manage" "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database" @@ -16,7 +18,24 @@ func main() { database.ConnectToDatabase() cache.Client = cache.NewRedisClient() - go cache.Client.ListenForMessages() + go Listen(cache.Client) http.StartServer() } + +func Listen(client cache.RedisClient) { + ch := make(chan cache.TicketMessage) + go client.ListenForMessages(ch) + + for decoded := range ch { + manage.SocketsLock.Lock() + for _, socket := range manage.Sockets { + if socket.Guild == decoded.GuildId && socket.Ticket == decoded.TicketId { + if err := socket.Ws.WriteJSON(decoded); err != nil { + fmt.Println(err.Error()) + } + } + } + manage.SocketsLock.Unlock() + } +}