Merge branch 'refs/heads/feat-video-stream'

This commit is contained in:
junleea 2024-12-26 13:59:12 +08:00
commit f092e3ac4d
1 changed files with 5 additions and 4 deletions

View File

@ -361,6 +361,7 @@ func VideoFeed(c *gin.Context) {
// 创建一个通道用于接收客户端连接关闭的信号
clientClosed := make(chan struct{})
defer close(clientClosed)
c.Stream(func(w io.Writer) bool {
// 将读取 Redis 消息通道的逻辑放在这里,方便根据返回值控制循环
ch := pubSub.Channel()
@ -384,22 +385,22 @@ func VideoFeed(c *gin.Context) {
_, err = w.Write([]byte("--frame\r\n"))
if err != nil {
fmt.Println("write video frame error:", err)
continue
return false
}
_, err = w.Write([]byte("Content-Type: image/jpeg\r\n\r\n"))
if err != nil {
fmt.Println("write video frame error:", err)
continue
return false
}
_, err = w.Write(buf)
if err != nil {
fmt.Println("write video frame error:", err)
continue
return false
}
_, err = w.Write([]byte("\r\n"))
if err != nil {
fmt.Println("write video frame error:", err)
continue
return false
}
case <-clientClosed:
// 当接收到客户端关闭连接的信号,返回 false 停止发送视频流