diff --git a/base/ws/msg.go b/base/ws/msg.go new file mode 100644 index 00000000..3e3a20ab --- /dev/null +++ b/base/ws/msg.go @@ -0,0 +1,27 @@ +package ws + +const SuccessMsgType = 1 +const ErrorMsgType = 0 +const InfoMsgType = 2 + +// websocket消息 +type Msg struct { + Type int `json:"type"` // 消息类型 + Title string `json:"title"` // 消息标题 + Msg string `json:"msg"` // 消息内容 +} + +// 普通消息 +func NewMsg(title, msg string) *Msg { + return &Msg{Type: InfoMsgType, Title: title, Msg: msg} +} + +// 成功消息 +func SuccessMsg(title, msg string) *Msg { + return &Msg{Type: SuccessMsgType, Title: title, Msg: msg} +} + +// 错误消息 +func ErrMsg(title, msg string) *Msg { + return &Msg{Type: ErrorMsgType, Title: title, Msg: msg} +} diff --git a/base/ws/ws.go b/base/ws/ws.go new file mode 100644 index 00000000..e88a504c --- /dev/null +++ b/base/ws/ws.go @@ -0,0 +1,32 @@ +package ws + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/websocket" +) + +var Upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024 * 1024 * 10, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +var conns = make(map[uint64]*websocket.Conn, 100) + +// 放置ws连接 +func Put(userId uint64, conn *websocket.Conn) { + conns[userId] = conn +} + +// 对指定用户发送消息 +func SendMsg(userId uint64, msg *Msg) { + conn := conns[userId] + if conn != nil { + bytes, _ := json.Marshal(msg) + conn.WriteMessage(websocket.TextMessage, bytes) + } +} diff --git a/mayfly_go_web/src/common/SocketBuilder.ts b/mayfly_go_web/src/common/SocketBuilder.ts new file mode 100644 index 00000000..c1bca8c4 --- /dev/null +++ b/mayfly_go_web/src/common/SocketBuilder.ts @@ -0,0 +1,47 @@ +class SocketBuilder { + + websocket: WebSocket; + + constructor(url: string) { + if (typeof (WebSocket) === "undefined") { + throw new Error('不支持websocket'); + } + if (!url) { + throw new Error('websocket url不能为空'); + } + console.log(url) + this.websocket = new WebSocket(url); + } + + static builder(url: string) { + return new SocketBuilder(url); + } + + open(onopen: any) { + this.websocket.onopen = onopen; + return this; + } + + error(onerror: any) { + this.websocket.onerror = onerror; + return this; + } + + message(onmessage: any) { + this.websocket.onmessage = onmessage; + return this; + } + + close(onclose: any) { + this.websocket.onclose = onclose; + return this; + } + + build() { + return this.websocket; + } + } + + + export default SocketBuilder; + \ No newline at end of file diff --git a/mayfly_go_web/src/common/sockets.ts b/mayfly_go_web/src/common/sockets.ts new file mode 100644 index 00000000..7178a81b --- /dev/null +++ b/mayfly_go_web/src/common/sockets.ts @@ -0,0 +1,44 @@ + +import Config from './config' +import { ElNotification } from 'element-plus' +import SocketBuilder from './SocketBuilder'; +import { getSession } from '@/common/utils/storage.ts'; + +export default { + /** + * 全局系统消息websocket + */ + sysMsgSocket() { + const token = getSession('token'); + if (!token) { + return null; + } + return SocketBuilder.builder(`${Config.baseWsUrl}/sysmsg?token=${token}`) + .message((event: { data: string }) => { + const message = JSON.parse(event.data); + let mtype: string; + switch (message.type) { + case 0: + mtype = 'error'; + break; + case 2: + mtype = 'info'; + break; + case 1: + mtype = 'success'; + break; + default: + mtype = 'info'; + } + if (mtype == undefined) { + return; + } + ElNotification({ + title: message.title, + message: message.msg, + type: mtype as any, + }) + }) + .open((event: any) => console.log(event)).build(); + } +} diff --git a/mayfly_go_web/src/router/index.ts b/mayfly_go_web/src/router/index.ts index 9c1c66ef..6ba0ba7f 100644 --- a/mayfly_go_web/src/router/index.ts +++ b/mayfly_go_web/src/router/index.ts @@ -8,6 +8,7 @@ import { NextLoading } from '@/common/utils/loading.ts'; import { dynamicRoutes, staticRoutes, pathMatch } from './route.ts' import { imports } from './imports'; import openApi from '@/common/openApi'; +import sockets from '@/common/sockets'; // 添加静态路由 const router = createRouter({ @@ -203,6 +204,9 @@ if (!isRequestRoutes) { initBackEndControlRoutesFun(); } + +let SysWs: any; + // 路由加载前 router.beforeEach((to, from, next) => { NProgress.configure({ showSpinner: false }); @@ -223,10 +227,18 @@ router.beforeEach((to, from, next) => { clearSession(); resetRoute(); NProgress.done(); + + if (SysWs) { + SysWs.close(); + SysWs = null; + } } else if (token && to.path === '/login') { next('/'); NProgress.done(); } else { + if (!SysWs) { + SysWs = sockets.sysMsgSocket(); + } if (store.state.routesList.routesList.length > 0) next(); } } diff --git a/mayfly_go_web/src/views/home/index.vue b/mayfly_go_web/src/views/home/index.vue index 7fa06f0a..ded294c9 100644 --- a/mayfly_go_web/src/views/home/index.vue +++ b/mayfly_go_web/src/views/home/index.vue @@ -93,9 +93,9 @@