From 44e9beca709d4e3b6f5f6d5fe5e979aefaae94ec Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 10:20:07 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=84=9A=E6=9C=ACls?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index 8fbeaad..4f64aa5 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -77,8 +77,6 @@ else git pull fi ` + cid.Script + ` -pwd -ls -lh echo "end"` //执行脚本 cmd := exec.Command("/bin/bash", "-c", scriptContent) @@ -219,8 +217,6 @@ else git pull fi ` + res.Script + ` -pwd -ls -lh echo "end"` //执行脚本 cmd := exec.Command("/bin/bash", "-c", scriptContent) From a54d1f93f1c2a4692465e385bbe517abf2692ef0 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 10:37:29 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/user.go b/handler/user.go index 35aa2c2..269078c 100644 --- a/handler/user.go +++ b/handler/user.go @@ -296,6 +296,6 @@ func registerHandler(c *gin.Context) { data["username"] = req_data.User data["email"] = req_data.Email data["token"] = tokenString - c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "date": data}) + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data}) return } From 6acfb4277e9dfa5d8310f7f82ba841602563a581 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 10:45:55 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF=E4=B8=8E?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handler/user.go b/handler/user.go index 269078c..5e1ffdc 100644 --- a/handler/user.go +++ b/handler/user.go @@ -254,7 +254,7 @@ func registerHandler(c *gin.Context) { var req_data RLReq tokenString := "" var id uint - if err := c.ShouldBindJSON(&req_data); err == nil { + if err := c.ShouldBind(&req_data); err == nil { if len(req_data.Password) != 32 { hasher := md5.New() hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值 @@ -281,7 +281,7 @@ func registerHandler(c *gin.Context) { return } } else { - c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"}) + c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "error"}) return } fmt.Println(req_data) From 87af0963fc29eb5cc3845a0bda5149f97c78cf3d Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 14:37:18 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 40 ++++++++++++++++++++++++++++++---------- handler/im.go | 1 + 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index 4f64aa5..56f0655 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -68,13 +68,23 @@ func RunCID(c *gin.Context) { //脚本内容,不同用户的持续集成、部署目录不同 scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + `/workspace/` + name + ` +TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + `/workspace/` + name + ` if [ ! -d $TARGET_DIR ]; then git clone ` + cid.Url + ` - cd $TARGET_DIR -else - cd $TARGET_DIR - git pull + if [ $? -ne 0 ]; then + echo "Failed to clone repository." + exit 1 + fi +fi +cd "$TARGET_DIR" +if [ $? -ne 0 ]; then + echo "Failed to change directory to $TARGET_DIR." + exit 1 +fi +git pull +if [ $? -ne 0 ]; then + echo "Failed to pull repository." + exit 1 fi ` + cid.Script + ` echo "end"` @@ -208,13 +218,23 @@ func CIDCallback(c *gin.Context) { user := dao.FindUserByUserID(res.Auth_id) scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + `workspace/` + name + ` +TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + `/workspace/` + name + ` if [ ! -d $TARGET_DIR ]; then git clone ` + res.Url + ` - cd $TARGET_DIR -else - cd $TARGET_DIR - git pull + if [ $? -ne 0 ]; then + echo "Failed to clone repository." + exit 1 + fi +fi +cd "$TARGET_DIR" +if [ $? -ne 0 ]; then + echo "Failed to change directory to $TARGET_DIR." + exit 1 +fi +git pull +if [ $? -ne 0 ]; then + echo "Failed to pull repository." + exit 1 fi ` + res.Script + ` echo "end"` diff --git a/handler/im.go b/handler/im.go index 7340624..d4552f9 100644 --- a/handler/im.go +++ b/handler/im.go @@ -144,6 +144,7 @@ func SRMessage(c *gin.Context) { } defer ws.Close() res := worker.GetRedis(redis_key + "_connection") + worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5) if res == "" { return } From 5bf7be12b5788f8909b240fb5cea3b4a540fe94b Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 14:42:52 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index 56f0655..84e434d 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -73,18 +73,15 @@ if [ ! -d $TARGET_DIR ]; then git clone ` + cid.Url + ` if [ $? -ne 0 ]; then echo "Failed to clone repository." - exit 1 fi fi cd "$TARGET_DIR" if [ $? -ne 0 ]; then echo "Failed to change directory to $TARGET_DIR." - exit 1 fi git pull if [ $? -ne 0 ]; then echo "Failed to pull repository." - exit 1 fi ` + cid.Script + ` echo "end"` @@ -223,18 +220,15 @@ if [ ! -d $TARGET_DIR ]; then git clone ` + res.Url + ` if [ $? -ne 0 ]; then echo "Failed to clone repository." - exit 1 fi fi cd "$TARGET_DIR" if [ $? -ne 0 ]; then echo "Failed to change directory to $TARGET_DIR." - exit 1 fi git pull if [ $? -ne 0 ]; then echo "Failed to pull repository." - exit 1 fi ` + res.Script + ` echo "end"` From 5a87d49be99c492814d7c843168abe3b07e05daf Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 15:25:11 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index 84e434d..dfb61cc 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -68,14 +68,14 @@ func RunCID(c *gin.Context) { //脚本内容,不同用户的持续集成、部署目录不同 scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + `/workspace/` + name + ` +TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + "/" + name + ` if [ ! -d $TARGET_DIR ]; then git clone ` + cid.Url + ` if [ $? -ne 0 ]; then echo "Failed to clone repository." fi fi -cd "$TARGET_DIR" +cd $TARGET_DIR if [ $? -ne 0 ]; then echo "Failed to change directory to $TARGET_DIR." fi @@ -215,14 +215,14 @@ func CIDCallback(c *gin.Context) { user := dao.FindUserByUserID(res.Auth_id) scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + `/workspace/` + name + ` +TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + "/" + name + ` if [ ! -d $TARGET_DIR ]; then - git clone ` + res.Url + ` + git clone ` + res.Url + ` $TARGET_DIR if [ $? -ne 0 ]; then echo "Failed to clone repository." fi fi -cd "$TARGET_DIR" +cd $TARGET_DIR if [ $? -ne 0 ]; then echo "Failed to change directory to $TARGET_DIR." fi From ec0a2667632a5453396a8e6e4d2e87e68fc7addc Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 15:35:38 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=87=BD=E6=95=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 95 +++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 63 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index dfb61cc..0bd0dda 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -60,45 +60,8 @@ func RunCID(c *gin.Context) { c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"}) return } else { - strs := strings.Split(cid.Url, "/") - name := strs[len(strs)-1] - names := strings.Split(name, ".") - name = names[0] - - //脚本内容,不同用户的持续集成、部署目录不同 - scriptContent := ` -echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + "/" + name + ` -if [ ! -d $TARGET_DIR ]; then - git clone ` + cid.Url + ` - if [ $? -ne 0 ]; then - echo "Failed to clone repository." - fi -fi -cd $TARGET_DIR -if [ $? -ne 0 ]; then - echo "Failed to change directory to $TARGET_DIR." -fi -git pull -if [ $? -ne 0 ]; then - echo "Failed to pull repository." -fi -` + cid.Script + ` -echo "end"` - //执行脚本 - cmd := exec.Command("/bin/bash", "-c", scriptContent) - // 使用bytes.Buffer捕获输出 - var out bytes.Buffer - cmd.Stdout = &out - err3 := cmd.Run() - err3_info := "" - if err3 != nil { - err3_info = err3.Error() - } - fmt.Println("bash content:", scriptContent) - dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3_info) //添加执行日志 + go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID) c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) - } } else { c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) @@ -207,17 +170,27 @@ func CIDCallback(c *gin.Context) { } res := dao.FindCIDByIDAndToken(cid, token) if res.ID != 0 { - //从url获取仓库名称 - strs := strings.Split(res.Url, "/") - name := strs[len(strs)-1] - names := strings.Split(name, ".") - name = names[0] - user := dao.FindUserByUserID(res.Auth_id) - scriptContent := ` + username, _ := c.Get("username") + go RunShell(username.(string), res.Url, res.Script, int(res.ID), res.Auth_id) + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) + } else { + c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"}) + return + } +} + +func RunShell(username, url, script string, id, authID int) { + strs := strings.Split(url, "/") + name := strs[len(strs)-1] + names := strings.Split(name, ".") + name = names[0] + + //脚本内容,不同用户的持续集成、部署目录不同 + scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + "/" + name + ` +TARGET_DIR=` + proto.CID_BASE_DIR + username + "/" + name + ` if [ ! -d $TARGET_DIR ]; then - git clone ` + res.Url + ` $TARGET_DIR + git clone ` + url + ` if [ $? -ne 0 ]; then echo "Failed to clone repository." fi @@ -230,22 +203,18 @@ git pull if [ $? -ne 0 ]; then echo "Failed to pull repository." fi -` + res.Script + ` +` + script + ` echo "end"` - //执行脚本 - cmd := exec.Command("/bin/bash", "-c", scriptContent) - // 使用bytes.Buffer捕获输出 - var out bytes.Buffer - cmd.Stdout = &out - err3 := cmd.Run() - err3_info := "" - if err3 != nil { - err3_info = err3.Error() - } - dao.CreateRunLog(cid, res.Auth_id, scriptContent, out.String(), err3_info) //添加执行日志 - c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) - } else { - c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"}) - return + //执行脚本 + cmd := exec.Command("/bin/bash", "-c", scriptContent) + // 使用bytes.Buffer捕获输出 + var out bytes.Buffer + cmd.Stdout = &out + err3 := cmd.Run() + err3_info := "" + if err3 != nil { + err3_info = err3.Error() } + fmt.Println("bash content:", scriptContent) + dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info) //添加执行日志 } From 77401657f58f18b1b418c3b4cb602e966404d312 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 15:48:40 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index 0bd0dda..85a6429 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -188,21 +188,6 @@ func RunShell(username, url, script string, id, authID int) { //脚本内容,不同用户的持续集成、部署目录不同 scriptContent := ` echo "start" -TARGET_DIR=` + proto.CID_BASE_DIR + username + "/" + name + ` -if [ ! -d $TARGET_DIR ]; then - git clone ` + url + ` - if [ $? -ne 0 ]; then - echo "Failed to clone repository." - fi -fi -cd $TARGET_DIR -if [ $? -ne 0 ]; then - echo "Failed to change directory to $TARGET_DIR." -fi -git pull -if [ $? -ne 0 ]; then - echo "Failed to pull repository." -fi ` + script + ` echo "end"` //执行脚本 From e7107660d645e288f6f6a6ca31f9748b3fee051f Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 8 Jul 2024 16:18:39 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=BB=BA=E7=AB=8B=E4=B8=8D=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/im.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/handler/im.go b/handler/im.go index d4552f9..0afec6b 100644 --- a/handler/im.go +++ b/handler/im.go @@ -209,10 +209,6 @@ func SRMessage(c *gin.Context) { err2 := ws.WriteMessage(websocket.TextMessage, res3) if err2 != nil { worker.SetRedisWithExpire("user_"+id.(string)+"_status", "0", time.Second*120) //设置用户在线状态,1为在线,0为离线,5秒后过期 - clientsMux.Lock() - delete(clients, ws) - clientsMux.Unlock() - break } else { worker.SetRedisWithExpire("user_"+id.(string)+"_status", "1", time.Second*5) //设置用户在线状态,1为在线,0为离线,5秒后过期 } From d692b8d9dffa303ac04972f9b47a7db827c47fd0 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 9 Jul 2024 09:59:19 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E9=A1=B5=EF=BC=8C=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/video.go | 7 +++++++ handler/cid.go | 1 + service/videoService.go | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/dao/video.go b/dao/video.go index f3d972a..638967d 100644 --- a/dao/video.go +++ b/dao/video.go @@ -108,3 +108,10 @@ func QuashAllDelay(user_id int, day int) int { } return int(res.RowsAffected) } + +// 获取视频列表分页 +func GetVideoListByPage(auth_id, page, pageSize int) []Video { + var videos []Video + DB.Debug().Where("auth_id = ? and isdelete = ?", auth_id, 0).Order("create_time DESC").Offset((page - 1) * pageSize).Limit(pageSize).Find(&videos) //Offset((page - 1) * pageSize).Limit(pageSize),分页,page从1开始,pageSize每页多少条,Offset是偏移量,Limit是限制条数 + return videos +} diff --git a/handler/cid.go b/handler/cid.go index 85a6429..e6d4809 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -162,6 +162,7 @@ func CIDCallback(c *gin.Context) { // 获取用户ID token := c.Query("token") cid_id := c.Query("id") + fmt.Println("token:", token, "cid_id:", cid_id) //将cid转换为int cid, _ := strconv.Atoi(cid_id) if token == "" || cid == 0 { diff --git a/service/videoService.go b/service/videoService.go index 2ee29a5..8527207 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -68,6 +68,16 @@ func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete return dao.CreateVideo(videoPath, videoName, cameraID, authID, human, isDelete, createTime, endTime, deleteTime, fileSize) } +func GetVideoListByPage(auth_id, page, page_size int) []dao.Video { + if page < 0 { + page = 0 //默认第一页 + } + if page_size < 0 || page_size > 100 { + page_size = 10 //默认每页10条 + } + return dao.GetVideoListByPage(auth_id, page, page_size) +} + func DeleteVideo(id, user int) int { return dao.DeleteVideoByID(id, user) } From 22d0bffdf3fcbb50f0bdf5a5571c7a3b8fc284a4 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 9 Jul 2024 10:10:03 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8D=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/cid.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handler/cid.go b/handler/cid.go index e6d4809..2108c95 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -171,8 +171,8 @@ func CIDCallback(c *gin.Context) { } res := dao.FindCIDByIDAndToken(cid, token) if res.ID != 0 { - username, _ := c.Get("username") - go RunShell(username.(string), res.Url, res.Script, int(res.ID), res.Auth_id) + user := dao.FindUserByID(res.Auth_id) + go RunShell(user[0].Name, res.Url, res.Script, int(res.ID), res.Auth_id) c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) } else { c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"}) From f626d89d3037bdc5c42a0d2464f3a0258c369ba1 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 12 Jul 2024 11:34:24 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E8=A7=86=E9=A2=91=E5=8A=9F=E8=83=BDws?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 1 + handler/device.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/dao/user.go b/dao/user.go index bce57da..5f171be 100644 --- a/dao/user.go +++ b/dao/user.go @@ -13,6 +13,7 @@ type User struct { Email string `gorm:"column:email"` Password string `gorm:"column:password"` Gender string `gorm:"column:gender"` + Role string `gorm:"column:role"` CreateTime string `gorm:"column:create_time"` UpdateTime string `gorm:"column:update_time"` } diff --git a/handler/device.go b/handler/device.go index ff84b36..bb66c47 100644 --- a/handler/device.go +++ b/handler/device.go @@ -1,11 +1,16 @@ package handler import ( + "encoding/json" "fmt" "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" "net/http" + "strconv" + "time" "videoplayer/proto" "videoplayer/service" + "videoplayer/worker" ) type DeviceAddReq struct { @@ -199,3 +204,64 @@ func Restart(ip string) bool { } 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 + } + } +} From 63b5b968b4631e84b4d4f8b579e62d9f6ea62629 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Wed, 17 Jul 2024 18:12:14 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8DIm=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=8F=91=E9=80=81Bug=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E6=96=B9=E7=A6=BB=E7=BA=BF=E8=87=AA=E5=8A=A8=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/im.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/handler/im.go b/handler/im.go index 0afec6b..4fc96ee 100644 --- a/handler/im.go +++ b/handler/im.go @@ -144,7 +144,7 @@ func SRMessage(c *gin.Context) { } defer ws.Close() 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 == "" { return } @@ -197,8 +197,8 @@ func SRMessage(c *gin.Context) { res3, _ = json.Marshal(msg) } //判断对方是否在线,若不在线则发送离线消息,否则正常发送消息 - if worker.IsContainKey(to_user_id+"_status") == true { - if worker.GetRedis(to_user_id+"_status") == "0" { + if worker.IsContainKey("user_"+to_user_id+"_status") == true { + if worker.GetRedis("user_"+to_user_id+"_status") == "0" { msg.Type = "offline" msg.Msg = "offline" msg.From_user_id = -1 @@ -208,9 +208,9 @@ func SRMessage(c *gin.Context) { } err2 := ws.WriteMessage(websocket.TextMessage, res3) 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 { - 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秒查询一次 } else {