admin-dashboard-backend/modules/cache/cache.go

65 lines
1.2 KiB
Go

package cache
import (
"janex/admin-dashboard-backend/modules/structs"
"sync"
"github.com/gofiber/websocket/v2"
)
var socketClients []*structs.SocketClient
var mu sync.RWMutex
func AddSocketClient(sessionId string, 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 = remove(socketClients, i)
break
}
}
mu.Unlock()
}
func remove(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 GetSocketClient(sessionId string) (socketClient *structs.SocketClient, ok bool) {
mu.RLock()
defer mu.RUnlock()
client, ok := socketClients[sessionId]
return client, ok
}
func GetSocketClientByConn(conn *websocket.Conn) (socketClient *structs.SocketClient, err error) {
mu.RLock()
defer mu.RUnlock()
for _, client := range socketClients {
if client.Conn == conn {
return client, nil
}
}
return nil, errors.New("Failed to find socket client by ws conn")
}*/