You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/controller/chat.go

368 lines
9.0 KiB

5 years ago
package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
5 years ago
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"log"
5 years ago
"net/http"
"time"
5 years ago
)
5 years ago
//聊天主界面
func ActionChatMain(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_main", nil)
5 years ago
}
5 years ago
//聊天客户端界面
5 years ago
func ActionChatPage(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_page", nil)
}
5 years ago
//咨询界面
5 years ago
func PageChat(c *gin.Context) {
html := tools.FileGetContent("html/chat_page.html")
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, html)
}
5 years ago
//获取在线用户
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
5 years ago
userInfo["username"] = clientNameList[uid]
result = append(result, userInfo)
}
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
}
5 years ago
type NoticeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
5 years ago
type TypeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
type KfMessage struct {
5 years ago
Kf_name string `json:"kf_name"`
Avatar string `json:"avatar"`
Kf_id string `json:"kf_id"`
Kf_group string `json:"kf_group"`
5 years ago
Time string `json:"time"`
5 years ago
Guest_id string `json:"guest_id"`
5 years ago
Content string `json:"content"`
5 years ago
}
type UserMessage struct {
From_avatar string `json:"from_avatar"`
From_id string `json:"from_id"`
From_name string `json:"from_name"`
To_id string `json:"to_id"`
To_name string `json:"to_name"`
5 years ago
Time string `json:"time"`
5 years ago
Content string `json:"content"`
}
//定时检测客户端是否在线
func init() {
upgrader=websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
sendPingToClient()
}
func NewChatServer(c *gin.Context){
conn,err:=upgrader.Upgrade(c.Writer,c.Request,nil)
if err != nil {
log.Print("upgrade:", err)
return
}
for {
//接受消息
var receive []byte
var recevString string
messageType, receive, err := conn.ReadMessage()
if err != nil {
log.Println(err)
return
}
recevString=string(receive)
log.Println("客户端:", recevString,messageType)
var typeMsg TypeMessage
var kfMsg KfMessage
var userMsg UserMessage
json.Unmarshal(receive, &typeMsg)
if typeMsg.Type == nil || typeMsg.Data == nil {
break
}
msgType := typeMsg.Type.(string)
msgData, _ := json.Marshal(typeMsg.Data)
switch msgType {
//获取当前在线的所有用户
case "getOnlineUsers":
getOnlineUser(conn,messageType)
//用户上线
case "userInit":
json.Unmarshal(msgData, &userMsg)
//用户id对应的连接
clientList[userMsg.From_id] = conn
clientNameList[userMsg.From_id] = userMsg.From_name
SendUserAllNotice()
//客服上线
case "kfOnline":
json.Unmarshal(msgData, &kfMsg)
//客服id对应的连接
kefuList[kfMsg.Kf_id] = conn
//发送给客户
if len(clientList) == 0 {
break
}
//for _, conn := range clientList {
// SendKefuOnline(kfMsg, conn)
//}
//发送给客服通知
//SendOnekfuAllNotice(w)
//客服接手
case "kfConnect":
json.Unmarshal(msgData, &kfMsg)
kefuList[kfMsg.Kf_id] = conn
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)
conn.WriteMessage(messageType,str)
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)
conn.WriteMessage(messageType,str)
}
}
}
//兼容之前的聊天服务
/*
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)
5 years ago
var typeMsg TypeMessage
var kfMsg KfMessage
var userMsg UserMessage
json.Unmarshal([]byte(receive), &typeMsg)
5 years ago
if typeMsg.Type == nil || typeMsg.Data == nil {
5 years ago
break
}
5 years ago
msgType := typeMsg.Type.(string)
msgData, _ := json.Marshal(typeMsg.Data)
5 years ago
switch msgType {
//获取当前在线的所有用户
case "getOnlineUsers":
getOnlineUser(w)
//用户上线
case "userInit":
5 years ago
json.Unmarshal(msgData, &userMsg)
//用户id对应的连接
5 years ago
clientList[userMsg.From_id] = w
5 years ago
clientNameList[userMsg.From_id] = userMsg.From_name
SendUserAllNotice()
//客服上线
case "kfOnline":
5 years ago
json.Unmarshal(msgData, &kfMsg)
//客服id对应的连接
5 years ago
kefuList[kfMsg.Kf_id] = w
//发送给客户
5 years ago
if len(clientList) == 0 {
5 years ago
break
}
for _, conn := range clientList {
5 years ago
SendKefuOnline(kfMsg, conn)
}
//发送给客服通知
//SendOnekfuAllNotice(w)
//客服接手
case "kfConnect":
5 years ago
json.Unmarshal(msgData, &kfMsg)
kefuList[kfMsg.Kf_id] = w
5 years ago
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id])
5 years ago
case "kfChatMessage":
5 years ago
json.Unmarshal(msgData, &kfMsg)
conn := clientList[kfMsg.Guest_id]
if kfMsg.Guest_id == "" || conn == nil {
return
}
5 years ago
msg := NoticeMessage{
5 years ago
Type: "kfChatMessage",
5 years ago
Data: KfMessage{
5 years ago
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,
},
}
5 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
case "chatMessage":
5 years ago
json.Unmarshal(msgData, &userMsg)
conn := kefuList[userMsg.To_id]
msg := NoticeMessage{
5 years ago
Type: "chatMessage",
5 years ago
Data: UserMessage{
5 years ago
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,
5 years ago
Time: time.Now().Format("2006-01-02 15:04:05"),
5 years ago
},
}
5 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
}
}
}
*/
5 years ago
//发送给客户客服上线
5 years ago
func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
sendMsg := TypeMessage{
Type: "kfOnline",
Data: KfMessage{
5 years ago
Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id,
Kf_group: kfMsg.Kf_group,
5 years ago
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: "客服上线",
},
}
jsonStrByte, _ := json.Marshal(sendMsg)
conn.WriteMessage(1,jsonStrByte)
}
5 years ago
//发送给所有客服客户上线
5 years ago
func SendOnekfuAllNotice(conn *websocket.Conn) {
result := make([]map[string]string, 0)
for uid, _ := range clientList {
userInfo := make(map[string]string)
userInfo["uid"] = uid
5 years ago
userInfo["username"] = clientNameList[uid]
result = append(result, userInfo)
}
5 years ago
msg := NoticeMessage{
Type: "notice",
5 years ago
Data: result,
}
5 years ago
str, _ := json.Marshal(msg)
conn.WriteMessage(1,str)
}
5 years ago
//定时给客户端发送消息判断客户端是否在线
5 years ago
func sendPingToClient() {
msg := TypeMessage{
Type: "ping",
}
go func() {
5 years ago
for {
log.Println("check online users...")
5 years ago
str, _ := json.Marshal(msg)
for uid, conn := range clientList {
err := conn.WriteMessage(1,str)
5 years ago
if err != nil {
delete(clientList, uid)
SendUserAllNotice()
}
}
5 years ago
time.Sleep(10 * time.Second)
}
}()
}
func SendUserAllNotice() {
if len(kefuList) != 0 {
//发送给客服通知
for _, conn := range kefuList {
msg := NoticeMessage{
Type: "notice",
}
str, _ := json.Marshal(msg)
conn.WriteMessage(1,str)
}
}
}
//获取当前的在线用户
func getOnlineUser(w *websocket.Conn,messageType int) {
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 := NoticeMessage{
Type: "getOnlineUsers",
Data: result,
}
str, _ := json.Marshal(msg)
w.WriteMessage(messageType,str)
}
5 years ago
var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string)
5 years ago
var kefuList = make(map[string]*websocket.Conn)