42 lines
776 B
Go
42 lines
776 B
Go
package ws
|
|
|
|
import cmap "github.com/orcaman/concurrent-map/v2"
|
|
|
|
var userMgr = newUserManager()
|
|
|
|
type userManager struct {
|
|
users 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.Set(userId, connId)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *userManager) GetConnId(userId int64) uint32 {
|
|
connId, _ := m.users.Get(userId)
|
|
return connId
|
|
}
|
|
|
|
func (m *userManager) Remove(userId int64) {
|
|
if userId < 1 {
|
|
return
|
|
}
|
|
m.users.Remove(userId)
|
|
}
|