diff --git a/base/ws/ws.go b/base/ws/ws.go index e88a504c..747749eb 100644 --- a/base/ws/ws.go +++ b/base/ws/ws.go @@ -2,7 +2,9 @@ package ws import ( "encoding/json" + "mayfly-go/base/global" "net/http" + "time" "github.com/gorilla/websocket" ) @@ -17,11 +19,43 @@ var Upgrader = websocket.Upgrader{ var conns = make(map[uint64]*websocket.Conn, 100) +func init() { + checkConn() +} + // 放置ws连接 func Put(userId uint64, conn *websocket.Conn) { + Delete(userId) conns[userId] = conn } +func checkConn() { + heartbeat := time.Duration(20) * time.Second + tick := time.NewTicker(heartbeat) + go func() { + for range tick.C { + // 遍历所有连接,ping失败的则删除掉 + for uid, conn := range conns { + err := conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(heartbeat/2)) + if err != nil { + global.Log.Info("删除ping失败的websocket连接:uid = ", uid) + Delete(uid) + return + } + } + } + }() +} + +// 删除ws连接 +func Delete(userid uint64) { + conn := conns[userid] + if conn != nil { + conn.Close() + delete(conns, userid) + } +} + // 对指定用户发送消息 func SendMsg(userId uint64, msg *Msg) { conn := conns[userId] diff --git a/mayfly_go_web/src/common/sockets.ts b/mayfly_go_web/src/common/sockets.ts index 7178a81b..c5ab840f 100644 --- a/mayfly_go_web/src/common/sockets.ts +++ b/mayfly_go_web/src/common/sockets.ts @@ -34,6 +34,7 @@ export default { return; } ElNotification({ + duration: 0, title: message.title, message: message.msg, type: mtype as any, diff --git a/mayfly_go_web/src/views/ops/machine/FileManage.vue b/mayfly_go_web/src/views/ops/machine/FileManage.vue index 1e9f2e52..80170f4c 100755 --- a/mayfly_go_web/src/views/ops/machine/FileManage.vue +++ b/mayfly_go_web/src/views/ops/machine/FileManage.vue @@ -79,6 +79,7 @@ />