This commit is contained in:
laodaming 2023-09-07 10:16:52 +08:00
parent bc3719065b
commit 6fe3c5148e

View File

@ -285,28 +285,32 @@ func ConsumeUserConnPoolCtlChanData(ctx context.Context) {
panic("ConsumeUserConnPoolCtlChanData ctx deadline")
}
}()
var (
data userConnPoolCtlChanItem
userKey string
)
for {
select {
case data := <-userConnPoolCtlChan:
key := getmapUserConnPoolUniqueId(data.userId, data.guestId)
case data = <-userConnPoolCtlChan:
userKey = getmapUserConnPoolUniqueId(data.userId, data.guestId)
switch data.option {
case 2: //发送消息
logx.Info("通过用户id索引发送消息")
mapUserUniqueId, ok := mapUserConnPool[key]
mapUserUniqueId, ok := mapUserConnPool[userKey]
if !ok {
logx.Info("通过用户id索引发送消息,连接不存在用户索引key:", key)
logx.Info("通过用户id索引发送消息,连接不存在用户索引key:", userKey)
continue
}
for uniqueId, _ := range mapUserUniqueId {
//根据uniqueId查询原始池中连接
mapConnPoolVal, ok := mapConnPool.Load(uniqueId)
if !ok {
logx.Info("通过用户id索引发送消息,连接不存在用户索引key:", key, " 原始uniqueId:", uniqueId)
logx.Info("通过用户id索引发送消息,连接不存在用户索引key:", userKey, " 原始uniqueId:", uniqueId)
continue
}
originConn, ok := mapConnPoolVal.(wsConnectItem)
if !ok {
logx.Error("通过用户id索引发送消息,断言原始连接失败用户索引key:", key, " 原始uniqueId:", uniqueId)
logx.Error("通过用户id索引发送消息,断言原始连接失败用户索引key:", userKey, " 原始uniqueId:", uniqueId)
continue
}
originConn.sendToOutChan(data.message)
@ -314,19 +318,17 @@ func ConsumeUserConnPoolCtlChanData(ctx context.Context) {
case 1: //添加
logx.Info("添加用户id索引标识", data.uniqueId)
//存在这个用户的map池子
if mapUserUniqueId, ok := mapUserConnPool[key]; ok {
if mapUserUniqueId, ok := mapUserConnPool[userKey]; ok {
mapUserUniqueId[data.uniqueId] = struct{}{}
} else {
mapUserConnPool[key] = make(map[string]struct{})
mapUserConnPool[key][data.uniqueId] = struct{}{}
mapUserConnPool[userKey] = make(map[string]struct{})
mapUserConnPool[userKey][data.uniqueId] = struct{}{}
}
case 0: //删除
logx.Info("删除用户id索引标识", data.uniqueId)
if mapUserUniqueId, ok := mapUserConnPool[key]; ok {
if mapUserUniqueId, ok := mapUserConnPool[userKey]; ok {
delete(mapUserUniqueId, data.uniqueId)
}
default:
}
}
}