|
|
|
@ -42,7 +42,6 @@ func NewKefuServer(c *gin.Context) {
|
|
|
|
|
messageType, receive, err := conn.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
go SendPingToKefuClient()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -55,53 +54,31 @@ func NewKefuServer(c *gin.Context) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func AddKefuToList(kefu *User) {
|
|
|
|
|
var newKefuConns = []*User{kefu}
|
|
|
|
|
kefuConns := KefuList[kefu.Id]
|
|
|
|
|
if kefuConns != nil {
|
|
|
|
|
for _, otherKefu := range kefuConns {
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "many pong",
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
err := otherKefu.Conn.WriteMessage(websocket.TextMessage, str)
|
|
|
|
|
if err == nil {
|
|
|
|
|
newKefuConns = append(newKefuConns, otherKefu)
|
|
|
|
|
}
|
|
|
|
|
oldUser, ok := KefuList[kefu.Id]
|
|
|
|
|
if oldUser != nil || ok {
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "close",
|
|
|
|
|
Data: kefu.Id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Println("xxxxxxxxxxxxxxxxxxxxxxxx", newKefuConns)
|
|
|
|
|
KefuList[kefu.Id] = newKefuConns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//给超管发消息
|
|
|
|
|
func SuperAdminMessage(str []byte) {
|
|
|
|
|
return
|
|
|
|
|
//给超管发
|
|
|
|
|
for _, kefuUsers := range KefuList {
|
|
|
|
|
for _, kefuUser := range kefuUsers {
|
|
|
|
|
if kefuUser.Role_id == "2" {
|
|
|
|
|
kefuUser.Conn.WriteMessage(websocket.TextMessage, str)
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
if err := oldUser.Conn.WriteMessage(websocket.TextMessage, str); err != nil {
|
|
|
|
|
oldUser.Conn.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
KefuList[kefu.Id] = kefu
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//给指定客服发消息
|
|
|
|
|
func OneKefuMessage(toId string, str []byte) {
|
|
|
|
|
//新版
|
|
|
|
|
mKefuConns, ok := KefuList[toId]
|
|
|
|
|
if ok && len(mKefuConns) > 0 {
|
|
|
|
|
for _, kefu := range mKefuConns {
|
|
|
|
|
kefu, ok := KefuList[toId]
|
|
|
|
|
if ok{
|
|
|
|
|
log.Println("OneKefuMessage lock")
|
|
|
|
|
kefu.Mux.Lock()
|
|
|
|
|
defer kefu.Mux.Unlock()
|
|
|
|
|
log.Println("OneKefuMessage unlock")
|
|
|
|
|
error := kefu.Conn.WriteMessage(websocket.TextMessage, str)
|
|
|
|
|
tools.Logger().Println("send_kefu_message", error, string(str))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SuperAdminMessage(str)
|
|
|
|
|
}
|
|
|
|
|
func KefuMessage(visitorId, content string, kefuInfo models.User) {
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
@ -126,22 +103,14 @@ func SendPingToKefuClient() {
|
|
|
|
|
Type: "many pong",
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
for kefuId, kfConns := range KefuList {
|
|
|
|
|
var newKefuConns = []*User{}
|
|
|
|
|
for _, kefuConn := range kfConns {
|
|
|
|
|
if kefuConn == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
kefuConn.Mux.Lock()
|
|
|
|
|
defer kefuConn.Mux.Unlock()
|
|
|
|
|
err := kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
|
|
|
|
|
if err == nil {
|
|
|
|
|
newKefuConns = append(newKefuConns, kefuConn)
|
|
|
|
|
}
|
|
|
|
|
for kefuId, kefu := range KefuList {
|
|
|
|
|
if kefu == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if len(newKefuConns) > 0 {
|
|
|
|
|
KefuList[kefuId] = newKefuConns
|
|
|
|
|
} else {
|
|
|
|
|
kefu.Mux.Lock()
|
|
|
|
|
defer kefu.Mux.Unlock()
|
|
|
|
|
err := kefu.Conn.WriteMessage(websocket.TextMessage, str)
|
|
|
|
|
if err == nil {
|
|
|
|
|
delete(KefuList, kefuId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|