fox/ws/userMgr.go

46 lines
798 B
Go

package ws
import (
"sync"
)
var userMgr = newUserManager()
type userManager struct {
users sync.Map // cmap.ConcurrentMap[int64, uint32]
}
func newUserManager() *userManager {
return &userManager{
//users: cmap.NewWithCustomShardingFunction[int64, uint32](func(key int64) uint32 {
// return uint32(key)
//}),
}
}
func (m *userManager) Add(connId uint32, userId int64) bool {
if userId < 1 {
return false
}
if conn, ok := wsMgr.Get(connId); ok {
conn.setUserId(userId)
m.users.Store(userId, connId)
return true
}
return false
}
func (m *userManager) GetConnId(userId int64) uint32 {
if connId, ok := m.users.Load(userId); ok {
return connId.(uint32)
}
return 0
}
func (m *userManager) Remove(userId int64) {
if userId < 1 {
return
}
m.users.Delete(userId)
}