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

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

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

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

@ -129,11 +129,21 @@ var app=new Vue({
content.avator = msg.avator;
content.name = msg.name;
content.content = replaceContent(msg.content);
content.is_kefu = false;
content.is_kefu = msg.is_kefu=="yes"? true:false;
content.time = msg.time;
if (msg.id == this.currentGuest) {
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, {
body: msg.content,
@ -144,12 +154,6 @@ var app=new Vue({
notification.close();
_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 = "";
});
let content = {}
content.avator = this.kfConfig.avator;
content.name = this.kfConfig.name;
content.content = replaceContent(this.messageContent);
content.is_kefu = true;
content.time = '';
this.msgList.push(content);
// let content = {}
// content.avator = this.kfConfig.avator;
// content.name = this.kfConfig.name;
// content.content = replaceContent(this.messageContent);
// content.is_kefu = true;
// content.time = '';
// this.msgList.push(content);
this.scrollBottom();
},
//处理当前在线用户列表

@ -10,6 +10,7 @@ import (
"net/http"
"strconv"
"sync"
"time"
)
type User struct {
@ -42,6 +43,7 @@ type ClientMessage struct {
City string `json:"city"`
ClientIp string `json:"client_ip"`
Refer string `json:"refer"`
IsKefu string `json:"is_kefu"`
}
var ClientList = make(map[string]*User)
@ -59,7 +61,7 @@ func init() {
return true
},
}
go UpdateVisitorStatusCron()
}
func SendServerJiang(content string) string {
noticeServerJiang, err := strconv.ParseBool(models.FindConfig("NoticeServerJiang"))
@ -75,3 +77,20 @@ func SendServerJiang(content string) string {
res := tools.Get(url)
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