package ws import ( "sync" ) var userMgr = newUserManager() type userManager struct { users sync.Map // map[int64]uint32 } func newUserManager() *userManager { return &userManager{} } 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 } // k:userid v:conn func (m *userManager) Rang(cb func(k, v any) bool) { m.users.Range(cb) } func (m *userManager) Remove(userId int64) { if userId < 1 { return } m.users.Delete(userId) }