54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"clickandjoin.app/websocketserver/modules/structs"
|
|
"github.com/gofiber/websocket/v2"
|
|
)
|
|
|
|
var socketClients = make(map[string]*structs.SocketClient)
|
|
var mu sync.RWMutex
|
|
|
|
func AddSocketClient(wsSessionId string, socketClient *structs.SocketClient) {
|
|
mu.Lock()
|
|
socketClients[wsSessionId] = socketClient
|
|
mu.Unlock()
|
|
}
|
|
|
|
func DeleteClient(wsSessionId string) {
|
|
mu.Lock()
|
|
delete(socketClients, wsSessionId)
|
|
mu.Unlock()
|
|
}
|
|
|
|
func GetSocketClients() map[string]*structs.SocketClient {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
return socketClients
|
|
}
|
|
|
|
func GetSocketClient(wsSessionId string) (socketClient *structs.SocketClient, ok bool) {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
client, ok := socketClients[wsSessionId]
|
|
|
|
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")
|
|
}
|