优化聊天代码

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

@ -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)

@ -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() {

Loading…
Cancel
Save