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:53:40 +08:00
|
|
|
users sync.Map // map[int64]uint32
|
2025-05-25 20:02:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newUserManager() *userManager {
|
2025-05-28 19:53:40 +08:00
|
|
|
return &userManager{}
|
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
|
|
|
}
|
|
|
|
|
2025-05-28 19:53:40 +08:00
|
|
|
// k:userid v:conn
|
|
|
|
func (m *userManager) Rang(cb func(k, v any) bool) {
|
|
|
|
m.users.Range(cb)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|