im修复用户在线及群在线消息转发

This commit is contained in:
junleea 2024-10-18 19:56:49 +08:00
parent 3e85b9e199
commit 6cded474a0
3 changed files with 19 additions and 7 deletions

View File

@ -540,6 +540,7 @@ func ServerSendMsgV2(c *gin.Context) {
//设置用户在线状态 //设置用户在线状态
worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "1", time.Second*60) worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "1", time.Second*60)
worker.SetRedisBitmap("im2_online_users", int64(user_id), 1) worker.SetRedisBitmap("im2_online_users", int64(user_id), 1)
worker.SetRedisSetAdd("im2_online_users_set", strconv.Itoa(user_id))
//发送消息 //发送消息
key := "user_" + strconv.Itoa(user_id) + "_msg_ids" key := "user_" + strconv.Itoa(user_id) + "_msg_ids"
@ -577,6 +578,7 @@ func ServerSendMsgV2(c *gin.Context) {
if err2 != nil { if err2 != nil {
worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "0", time.Second*3600) worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "0", time.Second*3600)
worker.SetRedisBitmap("im2_online_users", int64(user_id), 0) worker.SetRedisBitmap("im2_online_users", int64(user_id), 0)
worker.SetRedisSetRemove("im2_online_users_set", strconv.Itoa(user_id))
clientsMux.Lock() clientsMux.Lock()
delete(clients, ws) delete(clients, ws)
clientsMux.Unlock() clientsMux.Unlock()

View File

@ -78,19 +78,18 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content
err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content) err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content)
//获取群里的用户 //获取群里的用户
user_ids := worker.GetRedisSetMembers("group_" + strconv.Itoa(group_id) + "_users") //user_ids := worker.GetRedisSetMembers("group_" + strconv.Itoa(group_id) + "_users")
//在线的用户群id
online_user_ids := worker.GetRedisSetIntersect("im2_online_users_set", "group_"+strconv.Itoa(group_id)+"_users")
//users := dao.FindGroupUsers(group_id) //users := dao.FindGroupUsers(group_id)
for _, user_id := range user_ids { for _, user_id := range online_user_ids {
//判断是否是自己,不是则存入redis //判断是否是自己,不是则存入redis
id_, _ := strconv.Atoi(user_id) id_, _ := strconv.Atoi(user_id)
if id_ == from_id { if id_ == from_id {
continue continue
} }
res := worker.GetRedis("user_" + user_id + "_status_v2") //在线,存入redis
if res == "1" { worker.PushRedisListWithExpire("user_"+user_id+"_msg_ids", strconv.Itoa(int(id)), time.Second*300)
//在线,存入redis
worker.PushRedisListWithExpire("user_"+user_id+"_msg_ids", strconv.Itoa(int(id)), time.Second*300)
}
} }
case 3: case 3:
//user := dao.FindUserByID(to_id) //user := dao.FindUserByID(to_id)

View File

@ -303,6 +303,17 @@ func SetRedisSetRemove(key string, value string) bool {
return true return true
} }
// 获取两个set的交集
func GetRedisSetIntersect(key1 string, key2 string) []string {
ctx := context.Background()
val, err := redisClient.SInter(ctx, key1, key2).Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil
}
return val
}
// 查看set是否包含元素 // 查看set是否包含元素
func IsContainSet(key string, value string) bool { func IsContainSet(key string, value string) bool {
ctx := context.Background() ctx := context.Background()