fox/ws/userMgr.go

46 lines
798 B
Go
Raw Normal View History

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