87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package ws
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/tidwall/gjson"
|
|
"kefu/tools"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type MonitorConnection struct {
|
|
KefuName string
|
|
UinqId string
|
|
Title string
|
|
Url string
|
|
Refer string
|
|
StartTime string
|
|
ClientIp string
|
|
}
|
|
|
|
var MonitorList = make(map[*websocket.Conn]*MonitorConnection)
|
|
|
|
/**
|
|
网站监控
|
|
*/
|
|
func NewMonitorServer(c *gin.Context) {
|
|
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
if err != nil {
|
|
http.NotFound(c.Writer, c.Request)
|
|
log.Println("upgrade error:", err)
|
|
return
|
|
}
|
|
//获取GET参数
|
|
kefuName := c.Query("kefu_name")
|
|
if kefuName == "" {
|
|
conn.Close()
|
|
return
|
|
}
|
|
connection := &MonitorConnection{
|
|
UinqId: tools.Uuid(),
|
|
ClientIp: c.ClientIP(),
|
|
KefuName: kefuName,
|
|
StartTime: tools.DateDefault(time.Now()),
|
|
}
|
|
MonitorList[conn] = connection
|
|
conn.WriteMessage(websocket.TextMessage, []byte("ok"))
|
|
for {
|
|
//接受消息
|
|
var receive []byte
|
|
_, receive, err := conn.ReadMessage()
|
|
if err != nil {
|
|
msg := TypeMessage{
|
|
Type: "monitorOffline",
|
|
Data: connection,
|
|
}
|
|
str, _ := json.Marshal(msg)
|
|
OneKefuMessage(kefuName, str)
|
|
delete(MonitorList, conn)
|
|
return
|
|
}
|
|
|
|
msgType := gjson.Get(string(receive), "type").String()
|
|
|
|
switch msgType {
|
|
case "monitorOnline":
|
|
title := gjson.Get(string(receive), "data.title").String()
|
|
url := gjson.Get(string(receive), "data.url").String()
|
|
refer := gjson.Get(string(receive), "data.refer").String()
|
|
connection.Title = title
|
|
connection.Url = url
|
|
connection.Refer = refer
|
|
msg := TypeMessage{
|
|
Type: "monitorOnline",
|
|
Data: connection,
|
|
}
|
|
str, _ := json.Marshal(msg)
|
|
OneKefuMessage(kefuName, str)
|
|
case "cursor":
|
|
|
|
}
|
|
|
|
}
|
|
}
|