From d948979187663f786ddb7a226dbee5770c2b1a08 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Fri, 5 Jun 2020 11:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=81=8A=E5=A4=A9=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 81 +++++++++++++------------------------- static/html/chat_main.html | 12 ++++++ 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/controller/chat.go b/controller/chat.go index 47705ab..75f0a5b 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -31,6 +31,10 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) { }) w.Write(msg) } +type NoticeMessage struct { + Type interface{} `json:"type"` + Data interface{} `json:"data"` +} type TypeMessage struct { Type interface{} `json:"type"` Data interface{} `json:"data"` @@ -80,25 +84,8 @@ func ChatServer(w *websocket.Conn) { json.Unmarshal(msgData,&userMsg) //用户id对应的连接 clientList[userMsg.From_id] = w - if len(kefuList)==0{ - websocket.Message.Send(w, "无客服在线") - }else{ - //发送给客服通知s - for _, conn := range kefuList { - result := make([]map[string]string, 0) - for uid, _ := range clientList { - userInfo := make(map[string]string) - userInfo["uid"] = uid - result = append(result, userInfo) - } - msg:=NoticeMessage{ - Type: "notice", - Data:result, - } - str,_:=json.Marshal(msg);sendStr:=string(str) - websocket.Message.Send(conn,sendStr) - } - } + SendUserAllNotice() + //客服上线 case "kfOnline": json.Unmarshal(msgData,&kfMsg) @@ -171,41 +158,27 @@ func ChatServer(w *websocket.Conn) { } } } -//客户登陆和客服登陆发送的消息 -type Message struct { - Type interface{} - Uid interface{} - Name interface{} - Avatar interface{} - Group interface{} -} -type KfMessageData struct { - Kf_name interface{} `json:"kf_name"` - Avatar interface{} `json:"avatar"` - Kf_id interface{} `json:"kf_id"` - Time interface{} `json:"time"` - Content interface{} `json:"content"` -} -type UserMessageData struct { - From_avatar interface{} `json:"from_avatar"` - From_id interface{} `json:"from_id"` - From_name interface{} `json:"from_name"` - To_id interface{} `json:"to_id"` - To_name interface{} `json:"to_name"` - Content interface{} `json:"content"` -} -type ChatKfMessage struct { - Message_type interface{} `json:"message_type"` - Data KfMessageData `json:"data"` -} -type ChatUserMessage struct { - Message_type interface{} `json:"message_type"` - Data UserMessageData `json:"data"` -} -type NoticeMessage struct { - Type interface{} `json:"type"` - Data interface{} `json:"data"` +func SendUserAllNotice(){ + if len(kefuList)!=0{ + //发送给客服通知 + for _, conn := range kefuList { + result := make([]map[string]string, 0) + for uid, _ := range clientList { + userInfo := make(map[string]string) + userInfo["uid"] = uid + result = append(result, userInfo) + } + msg:=NoticeMessage{ + Type: "notice", + Data:result, + } + str,_:=json.Marshal(msg);sendStr:=string(str) + websocket.Message.Send(conn,sendStr) + } + } } - +func SendKefuOnline(){} +func SendUserChat(){} +func SendKefuChat(){} var clientList = make(map[string]*websocket.Conn) var kefuList = make(map[string]*websocket.Conn) \ No newline at end of file diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 00afcd7..75a7c06 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -148,6 +148,7 @@ this.msgListUser[redata.data[i].uid] = []; } } + } if (redata.type == "chatMessage") { @@ -177,6 +178,17 @@ buf[i] = this.msgListUser[guestId][i]; } this.msgList = buf; + + //发送给客户我在线 + let data = {}; + let mes = {} + mes.type = "kfOnline"; + data.kf_id = "kf_1"; + data.kf_name = "客服丽丽"; + data.avatar = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4217138672,2588039002&fm=26&gp=0.jpg"; + data.group = "1"; + mes.data = data; + this.socket.send(JSON.stringify(mes)); }, //发送给客户 chatToUser() {