From 3a7590ae8d6276c8dc9fb2363350f7a8a0d7c712 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Wed, 6 Nov 2024 11:59:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E5=AE=9E=E6=97=B6=E8=A7=86=E9=A2=91=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E5=85=B3=E9=97=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/device.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/handler/device.go b/handler/device.go index 0bf7d3f..9918ace 100644 --- a/handler/device.go +++ b/handler/device.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" + "github.com/google/uuid" "github.com/gorilla/websocket" "net/http" "strconv" @@ -232,6 +233,11 @@ func GetRealTimeImage(c *gin.Context) { func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) { ctx := context.Background() 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 ws.Close() ch := pubsub.Channel() @@ -263,8 +269,13 @@ func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) { clients[ws] = false clientsMux.Unlock() 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 } } + //查看是否还有其他连接,没有则设置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") + } }