Compare commits
7 Commits
8e894b003f
...
385afc7b9c
| Author | SHA1 | Date |
|---|---|---|
|
|
385afc7b9c | |
|
|
d7f7628d3f | |
|
|
94e6a5c5ab | |
|
|
2b2035b4df | |
|
|
63b5b968b4 | |
|
|
422e312b95 | |
|
|
f626d89d30 |
|
|
@ -13,6 +13,7 @@ type User struct {
|
||||||
Email string `gorm:"column:email"`
|
Email string `gorm:"column:email"`
|
||||||
Password string `gorm:"column:password"`
|
Password string `gorm:"column:password"`
|
||||||
Gender string `gorm:"column:gender"`
|
Gender string `gorm:"column:gender"`
|
||||||
|
Role string `gorm:"column:role"`
|
||||||
CreateTime string `gorm:"column:create_time"`
|
CreateTime string `gorm:"column:create_time"`
|
||||||
UpdateTime string `gorm:"column:update_time"`
|
UpdateTime string `gorm:"column:update_time"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
"videoplayer/service"
|
"videoplayer/service"
|
||||||
|
"videoplayer/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeviceAddReq struct {
|
type DeviceAddReq struct {
|
||||||
|
|
@ -52,6 +57,7 @@ func SetUpDeviceGroup(router *gin.Engine) {
|
||||||
deviceGroup.POST("/set_device_status", SetDeviceStatus)
|
deviceGroup.POST("/set_device_status", SetDeviceStatus)
|
||||||
deviceGroup.POST("/update_device", UpdateDevice)
|
deviceGroup.POST("/update_device", UpdateDevice)
|
||||||
deviceGroup.POST("/delete_device", DeleteDevice)
|
deviceGroup.POST("/delete_device", DeleteDevice)
|
||||||
|
deviceGroup.GET("/get_real_time_image", GetRealTimeImage)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -199,3 +205,64 @@ func Restart(ip string) bool {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 接收及发送消息
|
||||||
|
func GetRealTimeImage(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
id1 := int(id.(float64))
|
||||||
|
device_id := c.Query("device_id")
|
||||||
|
//字符串转int
|
||||||
|
device_id_int, _ := strconv.Atoi(device_id)
|
||||||
|
device := service.GetDevice(device_id_int, id1)
|
||||||
|
if device.ID == 0 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.DataNotFound, "message": "device not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//建立连接
|
||||||
|
// 升级HTTP连接为WebSocket连接
|
||||||
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
|
clients[ws] = true
|
||||||
|
if err != nil {
|
||||||
|
// log.Println(err)
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5) //设置播放状态
|
||||||
|
// 接收客户端消息并发送到指定用户
|
||||||
|
|
||||||
|
go func(ws *websocket.Conn, device_id int) {
|
||||||
|
|
||||||
|
}(ws, device_id_int)
|
||||||
|
|
||||||
|
for {
|
||||||
|
if v := clients[ws]; v == true {
|
||||||
|
res2 := worker.PopRedisListLeft(device_id + "_frames")
|
||||||
|
var res3 []byte
|
||||||
|
var msg proto.Message
|
||||||
|
if res2 != "" {
|
||||||
|
//若有消息则发送消息
|
||||||
|
msg.Type = "img"
|
||||||
|
msg.Msg = res2
|
||||||
|
msg.From_user_id = id1
|
||||||
|
res3, _ = json.Marshal(msg)
|
||||||
|
} else {
|
||||||
|
//若无消息则发送心跳包
|
||||||
|
msg.Type = "check"
|
||||||
|
msg.Msg = "check"
|
||||||
|
msg.From_user_id = -1
|
||||||
|
res3, _ = json.Marshal(msg)
|
||||||
|
}
|
||||||
|
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
||||||
|
if err2 != nil {
|
||||||
|
clientsMux.Lock()
|
||||||
|
clients[ws] = false
|
||||||
|
clientsMux.Unlock()
|
||||||
|
//设置ws关闭状态信息
|
||||||
|
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "0", time.Minute*5) //设置播放状态
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * 100) //设置延时100ms
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ func SRMessage(c *gin.Context) {
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
res := worker.GetRedis(redis_key + "_connection")
|
res := worker.GetRedis(redis_key + "_connection")
|
||||||
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5)
|
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "1", time.Second*5)
|
||||||
if res == "" {
|
if res == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -197,20 +197,20 @@ func SRMessage(c *gin.Context) {
|
||||||
res3, _ = json.Marshal(msg)
|
res3, _ = json.Marshal(msg)
|
||||||
}
|
}
|
||||||
//判断对方是否在线,若不在线则发送离线消息,否则正常发送消息
|
//判断对方是否在线,若不在线则发送离线消息,否则正常发送消息
|
||||||
//if worker.IsContainKey(to_user_id+"_status") == true {
|
if worker.IsContainKey("user_"+to_user_id+"_status") == true {
|
||||||
// if worker.GetRedis(to_user_id+"_status") == "0" {
|
if worker.GetRedis("user_"+to_user_id+"_status") == "0" {
|
||||||
// msg.Type = "offline"
|
msg.Type = "offline"
|
||||||
// msg.Msg = "offline"
|
msg.Msg = "offline"
|
||||||
// msg.From_user_id = -1
|
msg.From_user_id = -1
|
||||||
// msg.Session = res
|
msg.Session = res
|
||||||
// res3, _ = json.Marshal(msg)
|
res3, _ = json.Marshal(msg)
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
err2 := ws.WriteMessage(websocket.TextMessage, res3)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
} else {
|
} else {
|
||||||
worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
worker.SetRedisWithExpire("user_"+strconv.Itoa(id1)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * 1) // 每1秒查询一次
|
time.Sleep(time.Second * 1) // 每1秒查询一次
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue