ws提供遍历玩家功能

This commit is contained in:
liuxiaobo 2025-05-28 19:53:40 +08:00
parent 80d513739c
commit 77ed185e08
2 changed files with 15 additions and 8 deletions

View File

@ -7,15 +7,11 @@ import (
var userMgr = newUserManager()
type userManager struct {
users sync.Map // cmap.ConcurrentMap[int64, uint32]
users sync.Map // map[int64]uint32
}
func newUserManager() *userManager {
return &userManager{
//users: cmap.NewWithCustomShardingFunction[int64, uint32](func(key int64) uint32 {
// return uint32(key)
//}),
}
return &userManager{}
}
func (m *userManager) Add(connId uint32, userId int64) bool {
@ -37,6 +33,11 @@ func (m *userManager) GetConnId(userId int64) 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

View File

@ -47,8 +47,14 @@ func (m *wsManager) FindByUserId(userId int64) (*wsConnect, bool) {
return m.Get(connId)
}
func (m *wsManager) Rang(cb func(k, v any) bool) {
m.wsConnAll.Range(cb)
func (m *wsManager) Rang(cb func(conn IConn) bool) {
userMgr.Rang(func(_, v any) bool {
connId := v.(uint32)
if conn, ok := m.Get(connId); ok {
return cb(conn)
}
return true
})
}
func (m *wsManager) Count() int {