fox/ws/userMgr.go

53 lines
1.0 KiB
Go
Raw Permalink 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
2025-06-17 18:24:33 +08:00
//var userMgr = newUserManager()
2025-05-25 20:02:15 +08:00
type userManager struct {
2025-06-17 18:24:33 +08:00
users sync.Map // map[int64]uint32
connMgr *connManager
2025-05-25 20:02:15 +08:00
}
2025-06-17 18:24:33 +08:00
func newUserManager(connMgr *connManager) *userManager {
return &userManager{
connMgr: connMgr,
}
2025-05-25 20:02:15 +08:00
}
func (m *userManager) Add(connId uint32, userId int64) bool {
2025-06-17 18:24:33 +08:00
if conn, ok := m.connMgr.Get(connId); ok {
if conn.UserId() == 0 {
//log.DebugF("添加玩家:%v 连接:%v", userId, connId)
} else {
//log.DebugF("将连接:%v里的玩家id:%v设置为:%v", connId, conn.UserId(), userId)
}
2025-05-25 20:02:15 +08:00
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-06-17 18:24:33 +08:00
//log.DebugF("删除玩家:%v", userId)
2025-05-28 19:43:56 +08:00
m.users.Delete(userId)
2025-05-25 20:02:15 +08:00
}