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") + } }