|
|
|
@ -16,17 +16,20 @@ func ActionChatMain(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
render := tmpl.NewRender(w)
|
|
|
|
|
render.Display("chat_main", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//聊天客户端界面
|
|
|
|
|
func ActionChatPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
render := tmpl.NewRender(w)
|
|
|
|
|
render.Display("chat_page", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//咨询界面
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取在线用户
|
|
|
|
|
func ChatUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("content-type", "text/json;charset=utf-8;")
|
|
|
|
@ -43,6 +46,7 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
})
|
|
|
|
|
w.Write(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NoticeMessage struct {
|
|
|
|
|
Type interface{} `json:"type"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
@ -69,6 +73,7 @@ type UserMessage struct {
|
|
|
|
|
Time string `json:"time"`
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//定时检测客户端是否在线
|
|
|
|
|
func init() {
|
|
|
|
|
sendPingToClient()
|
|
|
|
@ -144,7 +149,8 @@ func ChatServer(w *websocket.Conn) {
|
|
|
|
|
Content: kfMsg.Content,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
case "chatMessage":
|
|
|
|
|
json.Unmarshal(msgData, &userMsg)
|
|
|
|
@ -161,11 +167,13 @@ func ChatServer(w *websocket.Conn) {
|
|
|
|
|
Time: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//发送给所有客服客户上线
|
|
|
|
|
func SendUserAllNotice() {
|
|
|
|
|
if len(kefuList) != 0 {
|
|
|
|
@ -174,11 +182,13 @@ func SendUserAllNotice(){
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
|
Type: "notice",
|
|
|
|
|
}
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//发送给客户客服上线
|
|
|
|
|
func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
|
|
|
|
|
sendMsg := TypeMessage{
|
|
|
|
@ -195,6 +205,7 @@ func SendKefuOnline(kfMsg KfMessage,conn *websocket.Conn){
|
|
|
|
|
jsonStrByte, _ := json.Marshal(sendMsg)
|
|
|
|
|
websocket.Message.Send(conn, string(jsonStrByte))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//发送给所有客服客户上线
|
|
|
|
|
func SendOnekfuAllNotice(conn *websocket.Conn) {
|
|
|
|
|
result := make([]map[string]string, 0)
|
|
|
|
@ -208,9 +219,11 @@ func SendOnekfuAllNotice(conn *websocket.Conn){
|
|
|
|
|
Type: "notice",
|
|
|
|
|
Data: result,
|
|
|
|
|
}
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(conn, sendStr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取当前的在线用户
|
|
|
|
|
func getOnlineUser(w *websocket.Conn) {
|
|
|
|
|
result := make([]map[string]string, 0)
|
|
|
|
@ -224,9 +237,11 @@ func getOnlineUser(w *websocket.Conn){
|
|
|
|
|
Type: "getOnlineUsers",
|
|
|
|
|
Data: result,
|
|
|
|
|
}
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
websocket.Message.Send(w, sendStr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//定时给客户端发送消息判断客户端是否在线
|
|
|
|
|
func sendPingToClient() {
|
|
|
|
|
msg := NoticeMessage{
|
|
|
|
@ -235,7 +250,8 @@ func sendPingToClient(){
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
log.Println("check online users...")
|
|
|
|
|
str,_:=json.Marshal(msg);sendStr:=string(str)
|
|
|
|
|
str, _ := json.Marshal(msg)
|
|
|
|
|
sendStr := string(str)
|
|
|
|
|
for uid, conn := range clientList {
|
|
|
|
|
err := websocket.Message.Send(conn, sendStr)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -248,6 +264,7 @@ func sendPingToClient(){
|
|
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientList = make(map[string]*websocket.Conn)
|
|
|
|
|
var clientNameList = make(map[string]string)
|
|
|
|
|
var kefuList = make(map[string]*websocket.Conn)
|