实时处理上下线

pull/30/head
taoshihan1991 4 years ago
parent e2fe179493
commit 3a6e37d22d

@ -20,6 +20,7 @@ type Message struct{
conn *websocket.Conn
c *gin.Context
content []byte
messageType int
}
var clientList = make(map[string]*vistor)
var kefuList = make(map[string]*websocket.Conn)
@ -48,7 +49,7 @@ func init() {
}
go sendPingUpdateStatus()
go singleBroadcaster()
go sendPingOnlineUsers()
//go sendPingOnlineUsers()
sendPingToClient()
}
@ -62,8 +63,26 @@ func NewChatServer(c *gin.Context){
//接受消息
var receive []byte
var recevString string
_, receive, err := conn.ReadMessage()
messageType, receive, err := conn.ReadMessage()
if err != nil {
for uid,visitor :=range clientList{
if visitor.conn==conn{
log.Println("删除用户",uid)
delete(clientList,uid)
userInfo := make(map[string]string)
userInfo["uid"] = uid
userInfo["name"] = visitor.name
msg := TypeMessage{
Type: "userOffline",
Data: userInfo,
}
str, _ := json.Marshal(msg)
kefuConn,ok:=kefuList[visitor.to_id]
if ok && kefuConn!=nil{
kefuConn.WriteMessage(websocket.TextMessage,str)
}
}
}
log.Println(err)
return
}
@ -73,6 +92,7 @@ func NewChatServer(c *gin.Context){
conn:conn,
content: receive,
c:c,
messageType:messageType,
}
}
}
@ -129,40 +149,36 @@ func sendPingUpdateStatus() {
}
//定时推送当前在线用户
func sendPingOnlineUsers() {
for {
var visitorIds []string
for visitorId, _ := range clientList {
visitorIds=append(visitorIds,visitorId)
}
sort.Strings(visitorIds)
var visitorIds []string
for visitorId, _ := range clientList {
visitorIds=append(visitorIds,visitorId)
}
sort.Strings(visitorIds)
for kefuId, kfConn := range kefuList {
result := make([]map[string]string, 0)
for _,visitorId:=range visitorIds{
user:=clientList[visitorId]
userInfo := make(map[string]string)
userInfo["uid"] = user.id
userInfo["username"] = user.name
userInfo["avator"] = user.avator
if user.to_id==kefuId{
result = append(result, userInfo)
}
}
msg := TypeMessage{
Type: "getOnlineUsers",
Data: result,
}
str, _ := json.Marshal(msg)
err:=kfConn.WriteMessage(websocket.TextMessage,str)
if err != nil {
delete(kefuList, kefuId)
for kefuId, kfConn := range kefuList {
result := make([]map[string]string, 0)
for _,visitorId:=range visitorIds{
user:=clientList[visitorId]
userInfo := make(map[string]string)
userInfo["uid"] = user.id
userInfo["username"] = user.name
userInfo["avator"] = user.avator
if user.to_id==kefuId{
result = append(result, userInfo)
}
}
time.Sleep(3 * time.Second)
msg := TypeMessage{
Type: "getOnlineUsers",
Data: result,
}
str, _ := json.Marshal(msg)
err:=kfConn.WriteMessage(websocket.TextMessage,str)
if err != nil {
delete(kefuList, kefuId)
}
}
}
//后端广播发送消息
func singleBroadcaster(){
for {
@ -193,6 +209,19 @@ func singleBroadcaster(){
clientList[clientMsg.Id] = user
//插入数据表
models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.Id,message.c.Request.Referer(),clientMsg.City,clientMsg.ClientIp)
userInfo := make(map[string]string)
userInfo["uid"] = user.id
userInfo["username"] = user.name
userInfo["avator"] = user.avator
msg := TypeMessage{
Type: "userOnline",
Data: userInfo,
}
str, _ := json.Marshal(msg)
kefuConn,ok:=kefuList[user.to_id]
if ok && kefuConn!=nil{
kefuConn.WriteMessage(websocket.TextMessage,str)
}
//客服上线
case "kfOnline":
json.Unmarshal(msgData, &clientMsg)
@ -202,11 +231,7 @@ func singleBroadcaster(){
if len(clientList) == 0 {
continue
}
//for _, conn := range clientList {
// SendKefuOnline(kfMsg, conn)
//}
//发送给客服通知
//SendOnekfuAllNotice(w)
sendPingOnlineUsers()
//客服接手
case "kfConnect":
json.Unmarshal(msgData, &clientMsg)

@ -218,6 +218,13 @@
this.handleOnlineUsers(redata.data);
//this.sendKefuOnline();
break;
case "userOnline":
this.users.push(redata.data);
break;
case "userOffline":
this.removeOfflineUser(redata.data);
//this.sendKefuOnline();
break;
case "notice":
// if(!this.usersMap[redata.data.uid]){
// this.$notify({
@ -298,6 +305,32 @@
this.scrollBottom();
},
//处理当前在线用户列表
handleOnlineUsers:function (retData) {
this.users = retData;
if (this.currentGuest == "") {
this.chatTitle = "连接成功,等待处理中...";
}
this.usersMap=[];
for(let i=0;i<retData.length;i++){
this.usersMap[retData[i].uid]=retData[i].username;
}
},
//处理当前在线用户列表
removeOfflineUser:function (retData) {
for(let i=0;i<this.users.length;i++){
if(this.users[i].uid==retData.uid){
this.users.splice(i,1);
}
}
let vid=retData.uid;
for(let i=0;i<this.visitors.length;i++){
if(this.visitors[i].visitor_id==vid){
this.visitors[i].status=0;
break;
}
}
},
//处理当前在线用户列表
handleOnlineUsers:function (retData) {
this.users = retData;
if (this.currentGuest == "") {

Loading…
Cancel
Save