实时修改用户状态

pull/30/head
taoshihan1991 4 years ago
parent 0716c2145b
commit 96eb54e7c9

@ -46,6 +46,7 @@ func init() {
}
go sendPingUpdateStatus()
go singleBroadcaster()
go sendPingOnlineUsers()
sendPingToClient()
}
@ -126,6 +127,28 @@ func sendPingUpdateStatus() {
time.Sleep(10 * time.Second)
}
}
//定时推送当前在线用户
func sendPingOnlineUsers() {
for {
result := make([]map[string]string, 0)
for _, user := range clientList {
userInfo := make(map[string]string)
userInfo["uid"] = user.id
userInfo["username"] = user.name
userInfo["avator"] = user.avator
result = append(result, userInfo)
}
msg := TypeMessage{
Type: "getOnlineUsers",
Data: result,
}
str, _ := json.Marshal(msg)
for _, kfConn := range kefuList {
kfConn.WriteMessage(websocket.TextMessage,str)
}
time.Sleep(10 * time.Second)
}
}
func SendNoticeToAllKefu() {
if len(kefuList) != 0 {
//发送给客服通知
@ -139,23 +162,7 @@ func SendNoticeToAllKefu() {
}
}
//获取当前的在线用户
func getOnlineUser(w *websocket.Conn) {
result := make([]map[string]string, 0)
for _, user := range clientList {
userInfo := make(map[string]string)
userInfo["uid"] = user.id
userInfo["username"] = user.name
userInfo["avator"] = user.avator
result = append(result, userInfo)
}
msg := TypeMessage{
Type: "getOnlineUsers",
Data: result,
}
str, _ := json.Marshal(msg)
w.WriteMessage(websocket.TextMessage,str)
}
//后端广播发送消息
func singleBroadcaster(){
for {
@ -172,9 +179,6 @@ func singleBroadcaster(){
msgType := typeMsg.Type.(string)
msgData, _ := json.Marshal(typeMsg.Data)
switch msgType {
//获取当前在线的所有用户
case "getOnlineUsers":
getOnlineUser(conn)
//用户上线
case "userInit":
json.Unmarshal(msgData, &clientMsg)
@ -209,14 +213,14 @@ func singleBroadcaster(){
kefuList[clientMsg.Id] = conn
visitor,ok := clientList[clientMsg.ToId]
if visitor==nil||!ok{
return
continue
}
SendKefuOnline(clientMsg, visitor.conn)
case "kfChatMessage":
json.Unmarshal(msgData, &clientMsg)
guest,ok:=clientList[clientMsg.ToId]
if guest==nil||!ok{
return
continue
}
conn := guest.conn
@ -237,7 +241,7 @@ func singleBroadcaster(){
json.Unmarshal(msgData, &clientMsg)
conn,ok := kefuList[clientMsg.ToId]
if conn==nil||!ok{
return
continue
}
msg := TypeMessage{
Type: "chatMessage",

@ -178,12 +178,6 @@
openUrl(url) {
window.location.href = url;
},
getOnlineUsers() {
let mes = {}
mes.type = "getOnlineUsers";
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
},
sendKefuOnline(){
let mes = {}
mes.type = "kfOnline";
@ -198,7 +192,7 @@
this.socket.onopen = this.OnOpen;
},
OnOpen() {
this.getOnlineUsers();
this.sendKefuOnline();
},
OnMessage(e) {
const redata = JSON.parse(e.data);
@ -216,7 +210,6 @@
// duration: 0,
// });
// }
this.getOnlineUsers();
this.sendKefuOnline();
break;
}
@ -312,12 +305,20 @@
if (this.currentGuest == "") {
this.chatTitle = "连接成功,等待处理中...";
}
this.usersMap=[];
for(let i=0;i<retData.length;i++){
this.usersMap[retData[i].uid]=retData[i].username;
if (typeof (this.msgListUser[retData[i].uid]) == "undefined") {
this.msgListUser[retData[i].uid] = [];
}
}
for(let i=0;i<this.visitors.length;i++){
let vid=this.visitors[i].visitor_id;
if(typeof this.usersMap[vid]=="undefined"){
this.visitors[i].status=0;
}
}
},
//获取客服信息
getKefuInfo(){

Loading…
Cancel
Save