修复多连接查看实时视频不正常关闭问题

This commit is contained in:
junleea 2024-11-06 11:59:53 +08:00
parent e1bae50a04
commit 3a7590ae8d
1 changed files with 12 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"net/http" "net/http"
"strconv" "strconv"
@ -232,6 +233,11 @@ func GetRealTimeImage(c *gin.Context) {
func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) { func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) {
ctx := context.Background() ctx := context.Background()
pubsub := worker.RedisClient.Subscribe(ctx, strconv.Itoa(device_id)+"_frames_msgs") pubsub := worker.RedisClient.Subscribe(ctx, strconv.Itoa(device_id)+"_frames_msgs")
//生成唯一连接uuid
con_id := uuid.New().String()
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
//加入设备在线连接集合
worker.SetRedisSetAdd(online_conn_key, con_id)
defer pubsub.Close() defer pubsub.Close()
defer ws.Close() defer ws.Close()
ch := pubsub.Channel() ch := pubsub.Channel()
@ -263,8 +269,13 @@ func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) {
clients[ws] = false clients[ws] = false
clientsMux.Unlock() clientsMux.Unlock()
fmt.Println("send message to client err:", err2) fmt.Println("send message to client err:", err2)
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "0", time.Minute*5) worker.SetRedisSetRemove(online_conn_key, con_id)
break break
} }
} }
//查看是否还有其他连接没有则设置is_play为0
if worker.GetRedisSetMembers(online_conn_key) == nil {
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "0", time.Minute*5)
fmt.Println("device_id:", device_id, " has set is_play to 0")
}
} }