From fd4e0c0495d57c48e94a61d30e34f7d6a0573b6d Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 10 Mar 2025 15:13:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/handler/tool.go b/handler/tool.go index c05f9c7..dcdde14 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -185,6 +185,55 @@ func GetRealTimeImage(c *gin.Context) { subscribeAndHandleMessagesV5(ws, deviceIdInt) } +func subscribeAndHandleMessagesV6(ws *websocket.Conn, deviceId int) { + // 生成唯一连接 uuid + con_id := uuid.New().String() + online_conn_key := "device_" + strconv.Itoa(deviceId) + "_online_conn_ids" + // 加入设备在线连接集合 + worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5) + //图片计数器 + count := 0 + //定时器,发送计数器 + tCount := 0 + var buf []byte + //计算帧率 + for { + //从service获取当前帧 + c := service.DeviceFrameCount[deviceId] + if c != count { + count = c + buf = service.Device1CurrentFrame.ToBytes() + err2 := ws.WriteMessage(websocket.BinaryMessage, buf) + if err2 != nil { + log.Printf("send message to client err:%v", err2) + worker.SetRedisSetRemove(online_conn_key, con_id) + break + } + c = count + } else { + //每秒发送一次心跳检测 + if tCount%10 == 0 { + err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second)) + if err != nil { + log.Printf("Connection check failed:%v", err) + worker.SetRedisSetRemove(online_conn_key, con_id) + break + } else { + log.Printf("Connection check success") + } + } + } + time.Sleep(100 * time.Millisecond) + tCount++ + } + + // 查看是否还有其他连接,没有则设置 is_play 为 0 + if worker.IsContainKey(online_conn_key) == false { + worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5) + log.Printf("device_id: %d has set is_play to 0", deviceId) + } +} + func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) { // 生成唯一连接 uuid con_id := uuid.New().String()