|
|
|
@ -4,37 +4,18 @@ import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
"github.com/taoshihan1991/imaptool/tools"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var clientList = make(map[string]*websocket.Conn)
|
|
|
|
|
var clientNameList = make(map[string]string)
|
|
|
|
|
var kefuList = make(map[string]*websocket.Conn)
|
|
|
|
|
|
|
|
|
|
//获取在线用户
|
|
|
|
|
func ChatUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("content-type", "text/json;charset=utf-8;")
|
|
|
|
|
result := make([]map[string]string, 0)
|
|
|
|
|
for uid, _ := range clientList {
|
|
|
|
|
userInfo := make(map[string]string)
|
|
|
|
|
userInfo["uid"] = uid
|
|
|
|
|
userInfo["username"] = clientNameList[uid]
|
|
|
|
|
result = append(result, userInfo)
|
|
|
|
|
}
|
|
|
|
|
msg, _ := json.Marshal(tools.JsonListResult{
|
|
|
|
|
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
|
|
|
|
|
Result: result,
|
|
|
|
|
})
|
|
|
|
|
w.Write(msg)
|
|
|
|
|
type vistor struct{
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
name string
|
|
|
|
|
id string
|
|
|
|
|
avator string
|
|
|
|
|
}
|
|
|
|
|
var clientList = make(map[string]*vistor)
|
|
|
|
|
var kefuList = make(map[string]*websocket.Conn)
|
|
|
|
|
|
|
|
|
|
type NoticeMessage struct {
|
|
|
|
|
Type interface{} `json:"type"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
type TypeMessage struct {
|
|
|
|
|
Type interface{} `json:"type"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
@ -101,9 +82,14 @@ func NewChatServer(c *gin.Context){
|
|
|
|
|
case "userInit":
|
|
|
|
|
json.Unmarshal(msgData, &userMsg)
|
|
|
|
|
//用户id对应的连接
|
|
|
|
|
clientList[userMsg.From_id] = conn
|
|
|
|
|
clientNameList[userMsg.From_id] = userMsg.From_name
|
|
|
|
|
SendUserAllNotice()
|
|
|
|
|
user:=&vistor{
|
|
|
|
|
conn:conn,
|
|
|
|
|
name: userMsg.From_name,
|
|
|
|
|
avator: userMsg.From_avatar,
|
|
|
|
|
id:userMsg.From_id,
|
|
|
|
|
}
|
|
|
|
|
clientList[userMsg.From_id] = user
|
|
|
|
|
SendNoticeToAllKefu()
|
|
|
|
|
//客服上线
|
|
|
|
|
case "kfOnline":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
@ -122,14 +108,14 @@ func NewChatServer(c *gin.Context){
|
|
|
|
|
case "kfConnect":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
|
kefuList[kfMsg.Kf_id] = conn
|
|
|
|
|
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id])
|
|
|
|
|
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id].conn)
|
|
|
|
|
case "kfChatMessage":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
|
conn := clientList[kfMsg.Guest_id]
|
|
|
|
|
conn := clientList[kfMsg.Guest_id].conn
|
|
|
|
|
if kfMsg.Guest_id == "" || conn == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "kfChatMessage",
|
|
|
|
|
Data: KfMessage{
|
|
|
|
|
Kf_name: kfMsg.Kf_name,
|
|
|
|
@ -145,7 +131,7 @@ func NewChatServer(c *gin.Context){
|
|
|
|
|
case "chatMessage":
|
|
|
|
|
json.Unmarshal(msgData, &userMsg)
|
|
|
|
|
conn := kefuList[userMsg.To_id]
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "chatMessage",
|
|
|
|
|
Data: UserMessage{
|
|
|
|
|
From_avatar: userMsg.From_avatar,
|
|
|
|
@ -162,102 +148,6 @@ func NewChatServer(c *gin.Context){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//兼容之前的聊天服务
|
|
|
|
|
/*
|
|
|
|
|
func ChatServer(w *websocket.Conn) {
|
|
|
|
|
var error error
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
//接受消息
|
|
|
|
|
var receive string
|
|
|
|
|
if error = websocket.Message.Receive(w, &receive); error != nil {
|
|
|
|
|
log.Println("接受消息失败", error)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
log.Println("客户端:", receive)
|
|
|
|
|
|
|
|
|
|
var typeMsg TypeMessage
|
|
|
|
|
var kfMsg KfMessage
|
|
|
|
|
var userMsg UserMessage
|
|
|
|
|
json.Unmarshal([]byte(receive), &typeMsg)
|
|
|
|
|
if typeMsg.Type == nil || typeMsg.Data == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
msgType := typeMsg.Type.(string)
|
|
|
|
|
msgData, _ := json.Marshal(typeMsg.Data)
|
|
|
|
|
|
|
|
|
|
switch msgType {
|
|
|
|
|
//获取当前在线的所有用户
|
|
|
|
|
case "getOnlineUsers":
|
|
|
|
|
getOnlineUser(w)
|
|
|
|
|
//用户上线
|
|
|
|
|
case "userInit":
|
|
|
|
|
json.Unmarshal(msgData, &userMsg)
|
|
|
|
|
//用户id对应的连接
|
|
|
|
|
clientList[userMsg.From_id] = w
|
|
|
|
|
clientNameList[userMsg.From_id] = userMsg.From_name
|
|
|
|
|
SendUserAllNotice()
|
|
|
|
|
//客服上线
|
|
|
|
|
case "kfOnline":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
|
//客服id对应的连接
|
|
|
|
|
kefuList[kfMsg.Kf_id] = w
|
|
|
|
|
//发送给客户
|
|
|
|
|
if len(clientList) == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
for _, conn := range clientList {
|
|
|
|
|
SendKefuOnline(kfMsg, conn)
|
|
|
|
|
}
|
|
|
|
|
//发送给客服通知
|
|
|
|
|
//SendOnekfuAllNotice(w)
|
|
|
|
|
//客服接手
|
|
|
|
|
case "kfConnect":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
|
kefuList[kfMsg.Kf_id] = w
|
|
|
|
|
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id])
|
|
|
|
|
case "kfChatMessage":
|
|
|
|
|
json.Unmarshal(msgData, &kfMsg)
|
|
|
|
|
conn := clientList[kfMsg.Guest_id]
|
|
|
|
|
if kfMsg.Guest_id == "" || conn == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
Type: "kfChatMessage",
|
|
|
|
|
Data: KfMessage{
|
|
|
|
|
Kf_name: kfMsg.Kf_name,
|
|
|
|
|
Avatar: kfMsg.Avatar,
|
|
|
|
|
Kf_id: kfMsg.Kf_id,
|
|
|
|
|
Time: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
|
Guest_id: kfMsg.Guest_id,
|
|
|
|
|
Content: kfMsg.Content,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
case "chatMessage":
|
|
|
|
|
json.Unmarshal(msgData, &userMsg)
|
|
|
|
|
conn := kefuList[userMsg.To_id]
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
Type: "chatMessage",
|
|
|
|
|
Data: UserMessage{
|
|
|
|
|
From_avatar: userMsg.From_avatar,
|
|
|
|
|
From_id: userMsg.From_id,
|
|
|
|
|
From_name: userMsg.From_name,
|
|
|
|
|
To_id: userMsg.To_id,
|
|
|
|
|
To_name: userMsg.To_name,
|
|
|
|
|
Content: userMsg.Content,
|
|
|
|
|
Time: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//发送给客户客服上线
|
|
|
|
|
func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
|
|
|
|
@ -279,13 +169,14 @@ func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
|
|
|
|
|
//发送给所有客服客户上线
|
|
|
|
|
func SendOnekfuAllNotice(conn *websocket.Conn) {
|
|
|
|
|
result := make([]map[string]string, 0)
|
|
|
|
|
for uid, _ := range clientList {
|
|
|
|
|
for _, user := range clientList {
|
|
|
|
|
userInfo := make(map[string]string)
|
|
|
|
|
userInfo["uid"] = uid
|
|
|
|
|
userInfo["username"] = clientNameList[uid]
|
|
|
|
|
userInfo["uid"] = user.id
|
|
|
|
|
userInfo["username"] = user.name
|
|
|
|
|
userInfo["avator"] = user.avator
|
|
|
|
|
result = append(result, userInfo)
|
|
|
|
|
}
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "notice",
|
|
|
|
|
Data: result,
|
|
|
|
|
}
|
|
|
|
@ -302,11 +193,11 @@ func sendPingToClient() {
|
|
|
|
|
for {
|
|
|
|
|
log.Println("check online users...")
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
for uid, conn := range clientList {
|
|
|
|
|
err := conn.WriteMessage(1,str)
|
|
|
|
|
for uid, user := range clientList {
|
|
|
|
|
err := user.conn.WriteMessage(1,str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
delete(clientList, uid)
|
|
|
|
|
SendUserAllNotice()
|
|
|
|
|
SendNoticeToAllKefu()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
@ -314,11 +205,11 @@ func sendPingToClient() {
|
|
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
func SendUserAllNotice() {
|
|
|
|
|
func SendNoticeToAllKefu() {
|
|
|
|
|
if len(kefuList) != 0 {
|
|
|
|
|
//发送给客服通知
|
|
|
|
|
for _, conn := range kefuList {
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "notice",
|
|
|
|
|
}
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
@ -330,13 +221,14 @@ func SendUserAllNotice() {
|
|
|
|
|
//获取当前的在线用户
|
|
|
|
|
func getOnlineUser(w *websocket.Conn,messageType int) {
|
|
|
|
|
result := make([]map[string]string, 0)
|
|
|
|
|
for uid, _ := range clientList {
|
|
|
|
|
for _, user := range clientList {
|
|
|
|
|
userInfo := make(map[string]string)
|
|
|
|
|
userInfo["uid"] = uid
|
|
|
|
|
userInfo["username"] = clientNameList[uid]
|
|
|
|
|
userInfo["uid"] = user.id
|
|
|
|
|
userInfo["username"] = user.name
|
|
|
|
|
userInfo["avator"] = user.avator
|
|
|
|
|
result = append(result, userInfo)
|
|
|
|
|
}
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
msg := TypeMessage{
|
|
|
|
|
Type: "getOnlineUsers",
|
|
|
|
|
Data: result,
|
|
|
|
|
}
|
|
|
|
|