添加redis bitmap设置,修改消息群消息发送
This commit is contained in:
parent
27cd64640f
commit
a7742b304e
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue