57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"jannex/admin-dashboard-backend/modules/structs"
|
|
"sync"
|
|
|
|
"github.com/gofiber/websocket/v2"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
var socketClients []*structs.SocketClient
|
|
var mu sync.RWMutex
|
|
|
|
func AddSocketClient(socketClient *structs.SocketClient) {
|
|
mu.Lock()
|
|
socketClients = append(socketClients, socketClient)
|
|
mu.Unlock()
|
|
}
|
|
|
|
func DeleteClientByConn(conn *websocket.Conn) {
|
|
mu.Lock()
|
|
|
|
for i := 0; i < len(socketClients); i++ {
|
|
if socketClients[i].Conn == conn {
|
|
socketClients = removeSocketClient(socketClients, i)
|
|
break
|
|
}
|
|
}
|
|
|
|
mu.Unlock()
|
|
}
|
|
|
|
func removeSocketClient(s []*structs.SocketClient, i int) []*structs.SocketClient {
|
|
return append(s[:i], s[i+1:]...)
|
|
}
|
|
|
|
func GetSocketClients() []*structs.SocketClient {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
return socketClients
|
|
}
|
|
|
|
func SubscribeSocketClientToTopic(sessionId string, topic string) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
log.Info().Msgf("Subscribing session %s to topic %s", sessionId, topic)
|
|
|
|
for _, socketClient := range socketClients {
|
|
if socketClient.SessionId == sessionId {
|
|
socketClient.SubscribedTopic = topic
|
|
break
|
|
}
|
|
}
|
|
}
|