区分在线和离线状态

pull/30/head
taoshihan1991 4 years ago
parent 758b8b9ffd
commit 0716c2145b

@ -21,7 +21,7 @@ type Message struct{
}
var clientList = make(map[string]*vistor)
var kefuList = make(map[string]*websocket.Conn)
var message = make(chan *Message, 10)
var message = make(chan *Message)
type TypeMessage struct {
Type interface{} `json:"type"`
@ -44,6 +44,7 @@ func init() {
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
go sendPingUpdateStatus()
go singleBroadcaster()
sendPingToClient()
}
@ -103,6 +104,7 @@ func sendPingToClient() {
err := user.conn.WriteMessage(websocket.TextMessage,str)
if err != nil {
delete(clientList, uid)
models.UpdateVisitorStatus(uid,0)
SendNoticeToAllKefu()
}
}
@ -111,6 +113,19 @@ func sendPingToClient() {
}()
}
//定时给更新数据库状态
func sendPingUpdateStatus() {
for {
visitors:=models.FindVisitorsOnline()
for _,visitor :=range visitors{
_,ok:=clientList[visitor.VisitorId]
if !ok{
models.UpdateVisitorStatus(visitor.VisitorId,0)
}
}
time.Sleep(10 * time.Second)
}
}
func SendNoticeToAllKefu() {
if len(kefuList) != 0 {
//发送给客服通知
@ -127,7 +142,6 @@ func SendNoticeToAllKefu() {
//获取当前的在线用户
func getOnlineUser(w *websocket.Conn) {
result := make([]map[string]string, 0)
log.Println(clientList)
for _, user := range clientList {
userInfo := make(map[string]string)
userInfo["uid"] = user.id
@ -146,12 +160,14 @@ func getOnlineUser(w *websocket.Conn) {
func singleBroadcaster(){
for {
message:=<-message
log.Println("debug:",message)
var typeMsg TypeMessage
var clientMsg ClientMessage
json.Unmarshal(message.content, &typeMsg)
conn:=message.conn
if typeMsg.Type == nil || typeMsg.Data == nil {
break
continue
}
msgType := typeMsg.Type.(string)
msgData, _ := json.Marshal(typeMsg.Data)
@ -180,7 +196,7 @@ func singleBroadcaster(){
kefuList[clientMsg.Id] = conn
//发送给客户
if len(clientList) == 0 {
break
continue
}
//for _, conn := range clientList {
// SendKefuOnline(kfMsg, conn)

@ -17,6 +17,8 @@ type Visitor struct {
func CreateVisitor(name string,avator string,sourceIp string,toId string,visitorId string,refer string,city string,clientIp string){
old:=FindVisitorByVistorId(visitorId)
if old.Name!=""{
//更新状态上线
UpdateVisitorStatus(visitorId,1)
return
}
v:=&Visitor{
@ -39,6 +41,16 @@ func FindVisitorByVistorId(visitorId string)Visitor{
}
func FindVisitors()[]Visitor{
var visitors []Visitor
DB.Find(&visitors)
DB.Order("status desc, updated_at desc").Find(&visitors)
return visitors
}
func FindVisitorsOnline()[]Visitor{
var visitors []Visitor
DB.Where("status = ?",1).Find(&visitors)
return visitors
}
func UpdateVisitorStatus(visitorId string,status uint){
visitor:=Visitor{
}
DB.Model(&visitor).Where("visitor_id = ?",visitorId).Update("status", status)
}

@ -33,11 +33,13 @@
### 安装使用
1. 先安装和运行mysql , 创建go-fly数据库并导入*.sql创建表结构与数据在config
1. 先安装和运行mysql , 创建go-fly数据库并导入*.sql创建表结构与数据.
2. 在go get github.com/taoshihan1991/go-fly ,进入$GOPATH/src/github.com/taoshihan1991/go-fly 目录
2. 在go get github.com/taoshihan1991/go-fly
进入$GOPATH/src/github.com/taoshihan1991/go-fly 目录
目录mysql.json中配置数据库
在config目录mysql.json中配置数据库
```php
{
"Server":"127.0.0.1",

@ -57,6 +57,7 @@
.chatBox{max-height: 350px;overflow-y: auto;overflow-x: hidden;}
.onlineUsers{padding: 5px;height: 40px;line-height: 40px;font-size: 14px;border-bottom: solid 1px #e6e6e6;}
.onlineUsers:hover,.onlineUsers.cur{background-color: #f0f9eb;color: #67C23A;}
.imgGray {-webkit-filter: grayscale(100%);-ms-filter: grayscale(100%);filter: grayscale(100%);filter: gray;color:#888;}
</style>
</head>
<body>
@ -82,9 +83,9 @@
<el-row v-for="item in visitors" :key="item.uid" class="">
<div style="cursor:pointer" class="onlineUsers" v-bind:class="{'cur': item.visitor_id==currentGuest }" v-on:click="talkTo(item.visitor_id,item.name)">
<el-col :span="4">
<el-avatar :size="40" :src="item.avator"></el-avatar>
<el-avatar v-bind:class="{'imgGray': item.status==0 }" :size="40" :src="item.avator"></el-avatar>
</el-col>
<el-col :span="16">
<el-col :span="16" v-bind:class="{'imgGray': item.status==0 }">
<{item.name}>
</el-col>
</div>

Loading…
Cancel
Save