给其他在线的账户连接发消息

pull/23/head
taoshihan1991 4 years ago
parent 902753b550
commit 94a618778b

@ -53,7 +53,7 @@ func init() {
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
} }
go UpdateVisitorStatusCron() //go UpdateVisitorStatusCron()
go singleBroadcaster() go singleBroadcaster()
//go sendPingOnlineUsers() //go sendPingOnlineUsers()
//sendPingToClient() //sendPingToClient()

@ -159,31 +159,32 @@ func SendMessageV2(c *gin.Context) {
msg = TypeMessage{ msg = TypeMessage{
Type: "message", Type: "message",
Data: ClientMessage{ Data: ws.ClientMessage{
Name: kefuInfo.Nickname, Name: kefuInfo.Nickname,
Avator: kefuInfo.Avator, Avator: kefuInfo.Avator,
Id: kefuInfo.Name, Id: kefuInfo.Name,
Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
ToId: vistorInfo.VisitorId, ToId: vistorInfo.VisitorId,
Content: content, Content: content,
IsKefu: "no",
}, },
} }
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)
conn.WriteMessage(websocket.TextMessage, str) conn.WriteMessage(websocket.TextMessage, str)
//msg = TypeMessage{ msg = TypeMessage{
// Type: "message", Type: "message",
// Data: ClientMessage{ Data: ws.ClientMessage{
// Name: kefuInfo.Nickname, Name: kefuInfo.Nickname,
// Avator: kefuInfo.Avator, Avator: kefuInfo.Avator,
// Id: vistorInfo.VisitorId, Id: vistorInfo.VisitorId,
// Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
// ToId: vistorInfo.VisitorId, ToId: vistorInfo.VisitorId,
// Content: content, Content: content,
// }, IsKefu: "yes",
//} },
//str2, _ := json.Marshal(msg) }
//ws.SuperAdminMessage(str2) str2, _ := json.Marshal(msg)
ws.OneKefuMessage(kefuInfo.Name, str2)
} }
if cType == "visitor" { if cType == "visitor" {
//kefuConns, ok := ws.KefuList[kefuInfo.Name] //kefuConns, ok := ws.KefuList[kefuInfo.Name]
@ -196,13 +197,14 @@ func SendMessageV2(c *gin.Context) {
//} //}
msg = TypeMessage{ msg = TypeMessage{
Type: "message", Type: "message",
Data: ClientMessage{ Data: ws.ClientMessage{
Avator: vistorInfo.Avator, Avator: vistorInfo.Avator,
Id: vistorInfo.VisitorId, Id: vistorInfo.VisitorId,
Name: vistorInfo.Name, Name: vistorInfo.Name,
ToId: kefuInfo.Name, ToId: kefuInfo.Name,
Content: content, Content: content,
Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
IsKefu: "no",
}, },
} }
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)

@ -129,11 +129,21 @@ var app=new Vue({
content.avator = msg.avator; content.avator = msg.avator;
content.name = msg.name; content.name = msg.name;
content.content = replaceContent(msg.content); content.content = replaceContent(msg.content);
content.is_kefu = false; content.is_kefu = msg.is_kefu=="yes"? true:false;
content.time = msg.time; content.time = msg.time;
if (msg.id == this.currentGuest) { if (msg.id == this.currentGuest) {
this.msgList.push(content); this.msgList.push(content);
} }
for(let i=0;i<this.users.length;i++){
if(this.users[i].uid==msg.id){
this.$set(this.users[i],'last_message',msg.content);
}
}
this.scrollBottom();
if(content.is_kefu){
return;
}
//发送通知 //发送通知
notify(msg.name, { notify(msg.name, {
body: msg.content, body: msg.content,
@ -144,12 +154,6 @@ var app=new Vue({
notification.close(); notification.close();
_this.talkTo(msg.id,msg.name); _this.talkTo(msg.id,msg.name);
}); });
for(let i=0;i<this.users.length;i++){
if(this.users[i].uid==msg.id){
this.$set(this.users[i],'last_message',msg.content);
}
}
this.scrollBottom();
} }
}, },
//接手客户 //接手客户
@ -186,13 +190,13 @@ var app=new Vue({
_this.messageContent = ""; _this.messageContent = "";
}); });
let content = {} // let content = {}
content.avator = this.kfConfig.avator; // content.avator = this.kfConfig.avator;
content.name = this.kfConfig.name; // content.name = this.kfConfig.name;
content.content = replaceContent(this.messageContent); // content.content = replaceContent(this.messageContent);
content.is_kefu = true; // content.is_kefu = true;
content.time = ''; // content.time = '';
this.msgList.push(content); // this.msgList.push(content);
this.scrollBottom(); this.scrollBottom();
}, },
//处理当前在线用户列表 //处理当前在线用户列表

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time"
) )
type User struct { type User struct {
@ -42,6 +43,7 @@ type ClientMessage struct {
City string `json:"city"` City string `json:"city"`
ClientIp string `json:"client_ip"` ClientIp string `json:"client_ip"`
Refer string `json:"refer"` Refer string `json:"refer"`
IsKefu string `json:"is_kefu"`
} }
var ClientList = make(map[string]*User) var ClientList = make(map[string]*User)
@ -59,7 +61,7 @@ func init() {
return true return true
}, },
} }
go UpdateVisitorStatusCron()
} }
func SendServerJiang(content string) string { func SendServerJiang(content string) string {
noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang")) noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang"))
@ -75,3 +77,20 @@ func SendServerJiang(content string) string {
res := tools.Get(url) res := tools.Get(url)
return res return res
} }
//定时给更新数据库状态
func UpdateVisitorStatusCron() {
for {
visitors := models.FindVisitorsOnline()
for _, visitor := range visitors {
if visitor.VisitorId == "" {
continue
}
_, ok := ClientList[visitor.VisitorId]
if !ok {
models.UpdateVisitorStatus(visitor.VisitorId, 0)
}
}
time.Sleep(60 * time.Second)
}
}

Loading…
Cancel
Save