Merge branch 'refs/heads/feat-realvp'
This commit is contained in:
commit
542935cb8f
|
|
@ -233,16 +233,36 @@ 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
|
// 生成唯一连接 uuid
|
||||||
con_id := uuid.New().String()
|
con_id := uuid.New().String()
|
||||||
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
|
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
|
||||||
//加入设备在线连接集合
|
// 加入设备在线连接集合
|
||||||
worker.SetRedisSetAdd(online_conn_key, con_id)
|
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
|
||||||
defer pubsub.Close()
|
defer pubsub.Close()
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
ch := pubsub.Channel()
|
ch := pubsub.Channel()
|
||||||
var check_cnt int
|
var check_cnt int
|
||||||
for msg := range ch {
|
for {
|
||||||
|
select {
|
||||||
|
case msg, ok := <-ch:
|
||||||
|
if !ok {
|
||||||
|
// 通道关闭或没有消息,每秒尝试检测连接
|
||||||
|
for {
|
||||||
|
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
||||||
|
if err != nil {
|
||||||
|
clientsMux.Lock()
|
||||||
|
clients[ws] = false
|
||||||
|
clientsMux.Unlock()
|
||||||
|
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||||
|
if worker.IsContainKey(online_conn_key) == false {
|
||||||
|
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "0", time.Minute*5)
|
||||||
|
fmt.Println("device_id:", device_id, " has set is_play to 0")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
var res3 []byte
|
var res3 []byte
|
||||||
var msgObj proto.Message
|
var msgObj proto.Message
|
||||||
if msg.Payload != "" {
|
if msg.Payload != "" {
|
||||||
|
|
@ -262,7 +282,7 @@ func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) {
|
||||||
msgObj.From_user_id = -1
|
msgObj.From_user_id = -1
|
||||||
res3, _ = json.Marshal(msgObj)
|
res3, _ = json.Marshal(msgObj)
|
||||||
}
|
}
|
||||||
//fmt.Println("send message to client length:", len(res3))
|
// fmt.Println("send message to client length:", len(res3))
|
||||||
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
clientsMux.Lock()
|
clientsMux.Lock()
|
||||||
|
|
@ -270,12 +290,9 @@ func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) {
|
||||||
clientsMux.Unlock()
|
clientsMux.Unlock()
|
||||||
fmt.Println("send message to client err:", err2)
|
fmt.Println("send message to client err:", err2)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//查看是否还有其他连接,没有则设置is_play为0
|
|
||||||
if worker.IsContainKey(online_conn_key) == false {
|
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "0", time.Minute*5)
|
|
||||||
fmt.Println("device_id:", device_id, " has set is_play to 0")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -292,6 +292,22 @@ func SetRedisSetAdd(key string, value string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置set,添加元素
|
||||||
|
func SetRedisSetAddWithExpire(key string, value string, expire time.Duration) bool {
|
||||||
|
ctx := context.Background()
|
||||||
|
err := RedisClient.SAdd(ctx, key, value).Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error setting key: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
err = RedisClient.Expire(ctx, key, expire).Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error setting key: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// 设置set,删除元素
|
// 设置set,删除元素
|
||||||
func SetRedisSetRemove(key string, value string) bool {
|
func SetRedisSetRemove(key string, value string) bool {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue