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

271 lines
6.6 KiB

4 years ago
package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
4 years ago
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"golang.org/x/net/websocket"
"log"
4 years ago
"net/http"
"time"
4 years ago
)
4 years ago
//聊天主界面
func ActionChatMain(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_main", nil)
4 years ago
}
4 years ago
//聊天客户端界面
4 years ago
func ActionChatPage(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_page", nil)
}
4 years ago
//咨询界面
4 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)
}
4 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
4 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)
}
4 years ago
type NoticeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
4 years ago
type TypeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
type KfMessage struct {
4 years ago
Kf_name string `json:"kf_name"`
Avatar string `json:"avatar"`
Kf_id string `json:"kf_id"`
Kf_group string `json:"kf_group"`
4 years ago
Time string `json:"time"`
4 years ago
Guest_id string `json:"guest_id"`
4 years ago
Content string `json:"content"`
4 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"`
4 years ago
Time string `json:"time"`
4 years ago
Content string `json:"content"`
}
4 years ago
//定时检测客户端是否在线
func init() {
sendPingToClient()
}
//兼容之前的聊天服务
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)
4 years ago
var typeMsg TypeMessage
var kfMsg KfMessage
var userMsg UserMessage
json.Unmarshal([]byte(receive), &typeMsg)
4 years ago
if typeMsg.Type == nil || typeMsg.Data == nil {
4 years ago
break
}
4 years ago
msgType := typeMsg.Type.(string)
msgData, _ := json.Marshal(typeMsg.Data)
4 years ago
switch msgType {
//获取当前在线的所有用户
case "getOnlineUsers":
getOnlineUser(w)
//用户上线
case "userInit":
4 years ago
json.Unmarshal(msgData, &userMsg)
//用户id对应的连接
4 years ago
clientList[userMsg.From_id] = w
4 years ago
clientNameList[userMsg.From_id] = userMsg.From_name
SendUserAllNotice()
//客服上线
case "kfOnline":
4 years ago
json.Unmarshal(msgData, &kfMsg)
//客服id对应的连接
4 years ago
kefuList[kfMsg.Kf_id] = w
//发送给客户
4 years ago
if len(clientList) == 0 {
4 years ago
break
}
for _, conn := range clientList {
4 years ago
SendKefuOnline(kfMsg, conn)
}
//发送给客服通知
//SendOnekfuAllNotice(w)
//客服接手
case "kfConnect":
4 years ago
json.Unmarshal(msgData, &kfMsg)
kefuList[kfMsg.Kf_id] = w
4 years ago
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id])
4 years ago
case "kfChatMessage":
4 years ago
json.Unmarshal(msgData, &kfMsg)
conn := clientList[kfMsg.Guest_id]
if kfMsg.Guest_id == "" || conn == nil {
return
}
4 years ago
msg := NoticeMessage{
4 years ago
Type: "kfChatMessage",
4 years ago
Data: KfMessage{
4 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,
},
}
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
case "chatMessage":
4 years ago
json.Unmarshal(msgData, &userMsg)
conn := kefuList[userMsg.To_id]
msg := NoticeMessage{
4 years ago
Type: "chatMessage",
4 years ago
Data: UserMessage{
4 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,
4 years ago
Time: time.Now().Format("2006-01-02 15:04:05"),
4 years ago
},
}
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
}
}
}
4 years ago
//发送给所有客服客户上线
4 years ago
func SendUserAllNotice() {
if len(kefuList) != 0 {
//发送给客服通知
for _, conn := range kefuList {
4 years ago
msg := NoticeMessage{
Type: "notice",
}
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
}
}
}
4 years ago
//发送给客户客服上线
4 years ago
func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
sendMsg := TypeMessage{
Type: "kfOnline",
Data: KfMessage{
4 years ago
Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id,
Kf_group: kfMsg.Kf_group,
4 years ago
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: "客服上线",
},
}
jsonStrByte, _ := json.Marshal(sendMsg)
websocket.Message.Send(conn, string(jsonStrByte))
}
4 years ago
//发送给所有客服客户上线
4 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
4 years ago
userInfo["username"] = clientNameList[uid]
result = append(result, userInfo)
}
4 years ago
msg := NoticeMessage{
Type: "notice",
4 years ago
Data: result,
}
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(conn, sendStr)
}
4 years ago
//获取当前的在线用户
4 years ago
func getOnlineUser(w *websocket.Conn) {
result := make([]map[string]string, 0)
for uid, _ := range clientList {
userInfo := make(map[string]string)
userInfo["uid"] = uid
4 years ago
userInfo["username"] = clientNameList[uid]
result = append(result, userInfo)
}
4 years ago
msg := NoticeMessage{
Type: "getOnlineUsers",
4 years ago
Data: result,
}
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
websocket.Message.Send(w, sendStr)
}
4 years ago
//定时给客户端发送消息判断客户端是否在线
4 years ago
func sendPingToClient() {
msg := NoticeMessage{
Type: "ping",
}
go func() {
4 years ago
for {
log.Println("check online users...")
4 years ago
str, _ := json.Marshal(msg)
sendStr := string(str)
for uid, conn := range clientList {
4 years ago
err := websocket.Message.Send(conn, sendStr)
if err != nil {
delete(clientList, uid)
SendUserAllNotice()
}
}
4 years ago
time.Sleep(10 * time.Second)
}
}()
}
4 years ago
var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string)
4 years ago
var kefuList = make(map[string]*websocket.Conn)