2021-11-11 15:56:02 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/biz"
|
2023-09-02 17:24:18 +08:00
|
|
|
"mayfly-go/pkg/logx"
|
2023-01-14 16:29:52 +08:00
|
|
|
"mayfly-go/pkg/req"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/ws"
|
2021-11-11 15:56:02 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type System struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 连接websocket
|
|
|
|
|
func (s *System) ConnectWs(g *gin.Context) {
|
|
|
|
|
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2023-09-02 17:24:18 +08:00
|
|
|
logx.ErrorTrace("websocket连接失败: ", err.(error))
|
2022-11-05 15:13:40 +08:00
|
|
|
if wsConn != nil {
|
|
|
|
|
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
|
|
|
|
|
wsConn.Close()
|
|
|
|
|
}
|
2021-11-11 15:56:02 +08:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2023-10-19 19:00:23 +08:00
|
|
|
biz.ErrIsNilAppendErr(err, "%s")
|
|
|
|
|
clientId := g.Query("clientId")
|
|
|
|
|
biz.NotEmpty(clientId, "clientId不能为空")
|
|
|
|
|
|
2021-11-11 15:56:02 +08:00
|
|
|
// 权限校验
|
2023-01-14 16:29:52 +08:00
|
|
|
rc := req.NewCtxWithGin(g)
|
|
|
|
|
if err = req.PermissionHandler(rc); err != nil {
|
2023-10-19 19:00:23 +08:00
|
|
|
panic("sys ws连接没有权限")
|
2021-11-11 15:56:02 +08:00
|
|
|
}
|
2021-11-12 14:35:34 +08:00
|
|
|
|
2021-11-11 15:56:02 +08:00
|
|
|
// 登录账号信息
|
|
|
|
|
la := rc.LoginAccount
|
2023-10-18 15:24:29 +08:00
|
|
|
if la != nil {
|
2023-10-19 19:00:23 +08:00
|
|
|
ws.AddClient(ws.UserId(la.Id), clientId, wsConn)
|
2023-10-18 15:24:29 +08:00
|
|
|
}
|
2021-11-11 15:56:02 +08:00
|
|
|
}
|