优化聊天代码

pull/30/head
taoshihan1991 4 years ago
parent c832e6cd5a
commit d948979187

@ -31,6 +31,10 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) {
}) })
w.Write(msg) w.Write(msg)
} }
type NoticeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
type TypeMessage struct { type TypeMessage struct {
Type interface{} `json:"type"` Type interface{} `json:"type"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
@ -80,25 +84,8 @@ func ChatServer(w *websocket.Conn) {
json.Unmarshal(msgData,&userMsg) json.Unmarshal(msgData,&userMsg)
//用户id对应的连接 //用户id对应的连接
clientList[userMsg.From_id] = w clientList[userMsg.From_id] = w
if len(kefuList)==0{ SendUserAllNotice()
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)
}
}
//客服上线 //客服上线
case "kfOnline": case "kfOnline":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData,&kfMsg)
@ -171,41 +158,27 @@ func ChatServer(w *websocket.Conn) {
} }
} }
} }
//客户登陆和客服登陆发送的消息 func SendUserAllNotice(){
type Message struct { if len(kefuList)!=0{
Type interface{} //发送给客服通知
Uid interface{} for _, conn := range kefuList {
Name interface{} result := make([]map[string]string, 0)
Avatar interface{} for uid, _ := range clientList {
Group interface{} userInfo := make(map[string]string)
} userInfo["uid"] = uid
type KfMessageData struct { result = append(result, userInfo)
Kf_name interface{} `json:"kf_name"` }
Avatar interface{} `json:"avatar"` msg:=NoticeMessage{
Kf_id interface{} `json:"kf_id"` Type: "notice",
Time interface{} `json:"time"` Data:result,
Content interface{} `json:"content"` }
} str,_:=json.Marshal(msg);sendStr:=string(str)
type UserMessageData struct { websocket.Message.Send(conn,sendStr)
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 SendKefuOnline(){}
func SendUserChat(){}
func SendKefuChat(){}
var clientList = make(map[string]*websocket.Conn) var clientList = make(map[string]*websocket.Conn)
var kefuList = make(map[string]*websocket.Conn) var kefuList = make(map[string]*websocket.Conn)

@ -148,6 +148,7 @@
this.msgListUser[redata.data[i].uid] = []; this.msgListUser[redata.data[i].uid] = [];
} }
} }
} }
if (redata.type == "chatMessage") { if (redata.type == "chatMessage") {
@ -177,6 +178,17 @@
buf[i] = this.msgListUser[guestId][i]; buf[i] = this.msgListUser[guestId][i];
} }
this.msgList = buf; 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() { chatToUser() {

Loading…
Cancel
Save