From 757a6c7c700b24cb36f1da2d1d940895254c53ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Thu, 24 Sep 2020 21:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BE=A4=E5=8F=91=E9=80=9A?= =?UTF-8?q?=E7=9F=A5http=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 16 +++++++++++++++- controller/message.go | 22 ++++++++++++++++++++++ router/api.go | 1 + static/js/chat-page.js | 9 +++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/controller/chat.go b/controller/chat.go index d361e2b..b6e811a 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -119,6 +119,15 @@ func SendKefuOnline(clientMsg ClientMessage, conn *websocket.Conn) { jsonStrByte, _ := json.Marshal(sendMsg) conn.WriteMessage(websocket.TextMessage,jsonStrByte) } +//发送通知 +func SendNotice(msg string, conn *websocket.Conn) { + sendMsg := TypeMessage{ + Type: "notice", + Data: msg, + } + jsonStrByte, _ := json.Marshal(sendMsg) + conn.WriteMessage(websocket.TextMessage,jsonStrByte) +} //定时给客户端发送消息判断客户端是否在线 func sendPingToClient() { @@ -231,6 +240,10 @@ func singleBroadcaster(){ //用户上线 case "userInit": json.Unmarshal(msgData, &clientMsg) + vistorInfo:=models.FindVisitorByVistorId(clientMsg.VisitorId) + if vistorInfo.VisitorId==""{ + SendNotice("访客数据不存在",conn) + } //用户id对应的连接 user:=&vistor{ conn:conn, @@ -241,7 +254,8 @@ func singleBroadcaster(){ } clientList[clientMsg.VisitorId] = user //插入数据表 - models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.VisitorId,clientMsg.Refer,clientMsg.City,clientMsg.ClientIp) + models.UpdateVisitor(clientMsg.VisitorId,1,clientMsg.ClientIp,message.c.ClientIP(),clientMsg.Refer) + //models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.VisitorId,clientMsg.Refer,clientMsg.City,clientMsg.ClientIp) userInfo := make(map[string]string) userInfo["uid"] = user.id userInfo["username"] = user.name diff --git a/controller/message.go b/controller/message.go index bc6ea5e..f26ef08 100644 --- a/controller/message.go +++ b/controller/message.go @@ -112,6 +112,28 @@ func SendMessage(c *gin.Context) { "msg": "ok", }) } +func SendVisitorNotice(c *gin.Context) { + notice := c.Query("msg") + if notice==""{ + c.JSON(200, gin.H{ + "code": 400, + "msg": "msg不能为空", + }) + return + } + msg := TypeMessage{ + Type: "notice", + Data: notice, + } + str, _ := json.Marshal(msg) + for _,visitor :=range clientList{ + visitor.conn.WriteMessage(websocket.TextMessage,str) + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} func UploadImg(c *gin.Context){ config:=config.CreateConfig() f, err := c.FormFile("imgfile") diff --git a/router/api.go b/router/api.go index 22cea9a..533717a 100644 --- a/router/api.go +++ b/router/api.go @@ -15,6 +15,7 @@ func InitApiRouter(engine *gin.Engine){ engine.GET("/chat_server",middleware.Ipblack, controller.NewChatServer) //获取消息 engine.GET("/messages", controller.GetVisitorMessage) + engine.GET("/message_notice", controller.SendVisitorNotice) //发送单条消息 engine.POST("/message",middleware.Ipblack,controller.SendMessage) //上传文件 diff --git a/static/js/chat-page.js b/static/js/chat-page.js index 1a2d223..ccec008 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -43,6 +43,15 @@ new Vue({ this.scrollBottom(); this.showKfonline=true; } + if (redata.type == "notice") { + let msg = redata.data + if(!msg){ + return; + } + this.chatTitle=msg + $(".chatBox").append("