diff --git a/handler/im.go b/handler/im.go index aaa4b5d..b0ec3ee 100644 --- a/handler/im.go +++ b/handler/im.go @@ -70,7 +70,7 @@ func SetUpIMGroup(router *gin.Engine) { imGroup.POST("/get_group", GetGroups) imGroup.POST("/get_group_req_user", GetFriendRequest) imGroup.GET("/sse_msg", ServerSendMsg) - imGroup.GET("/ws_v2", ServerSsendMsgV2) + imGroup.GET("/ws_v2", ServerSendMsgV2) imGroup.POST("/get_friend_list", GetFriendList) //获取好友列表,包括群聊 //获取好友请求 imGroup.POST("/get_friend_request", GetFriendRequest) @@ -432,7 +432,7 @@ func SRMessage(c *gin.Context) { } } -func ServerSsendMsgV2(c *gin.Context) { +func ServerSendMsgV2(c *gin.Context) { //wss id, _ := c.Get("id") user_id := int(id.(float64)) @@ -448,8 +448,8 @@ func ServerSsendMsgV2(c *gin.Context) { defer ws.Close() //设置用户在线状态 worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "1", time.Second*60) + worker.SetRedisBitmap("im2_online_users", int64(user_id), 1) //发送消息 - key := "user_" + strconv.Itoa(user_id) + "_msg_ids" for { @@ -485,6 +485,7 @@ func ServerSsendMsgV2(c *gin.Context) { err2 := ws.WriteMessage(websocket.TextMessage, res3) if err2 != nil { worker.SetRedisWithExpire("user_"+strconv.Itoa(user_id)+"_status_v2", "0", time.Second*3600) + worker.SetRedisBitmap("im2_online_users", int64(user_id), 0) clientsMux.Lock() delete(clients, ws) clientsMux.Unlock() diff --git a/service/imService.go b/service/imService.go index eca1aed..617cf63 100644 --- a/service/imService.go +++ b/service/imService.go @@ -50,8 +50,9 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content if from_id == 0 || group_id == 0 || content == "" { return errors.New("参数错误"), 0 } + key := "group_" + strconv.Itoa(group_id) + "_users" //判断是否在群里 - if worker.IsContainKey("group_"+strconv.Itoa(group_id)+"_users") == false { + if worker.IsContainKey(key) == false { //设置群缓存 isSuccess := SetGroupCache(group_id) //设置失败,直接查询数据库 @@ -77,15 +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) //获取群里的用户 - users := dao.FindGroupUsers(group_id) - for _, user := range users { - if user.UserID == from_id { + user_ids := worker.GetRedisSetMembers("group_" + strconv.Itoa(group_id) + "_users") + //users := dao.FindGroupUsers(group_id) + for _, user_id := range user_ids { + //判断是否是自己,不是则存入redis + id_, _ := strconv.Atoi(user_id) + if id_ == from_id { continue } - res := worker.GetRedis("user_" + strconv.Itoa(user.UserID) + "_status_v2") + res := worker.GetRedis("user_" + user_id + "_status_v2") if res == "1" { //在线,存入redis - worker.PushRedisListWithExpire("user_"+strconv.Itoa(user.UserID)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300) + worker.PushRedisListWithExpire("user_"+user_id+"_msg_ids", strconv.Itoa(int(id)), time.Second*300) } } case 3: diff --git a/worker/redis.go b/worker/redis.go index e2ff908..709790c 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -313,3 +313,36 @@ func IsContainSet(key string, value string) bool { } return val } + +// 查看set的所有元素 +func GetRedisSetMembers(key string) []string { + ctx := context.Background() + val, err := redisClient.SMembers(ctx, key).Result() + if err != nil { + fmt.Println("Error getting key: %v", err) + return nil + } + return val +} + +// BITMAP +func SetRedisBitmap(key string, offset int64, value int) bool { + ctx := context.Background() + err := redisClient.SetBit(ctx, key, offset, value).Err() + if err != nil { + fmt.Println("Error setting key: %v", err) + return false + } + return true +} + +// BITMAP获取 +func GetRedisBitmap(key string, offset int64) int { + ctx := context.Background() + val, err := redisClient.GetBit(ctx, key, offset).Result() + if err != nil { + fmt.Println("Error getting key: %v", err) + return 0 + } + return int(val) +}