59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"fusenapi/server/websocket/internal/svc"
|
||
|
"github.com/gorilla/websocket"
|
||
|
"net/http"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
//升级
|
||
|
upgrade = websocket.Upgrader{
|
||
|
//允许跨域
|
||
|
CheckOrigin: func(r *http.Request) bool {
|
||
|
return true
|
||
|
},
|
||
|
}
|
||
|
//连接map池
|
||
|
mapConn = sync.Map{}
|
||
|
)
|
||
|
|
||
|
type wsConnectItem struct {
|
||
|
conn *websocket.Conn //websocket的连接
|
||
|
renImage sync.Map //需要渲染的图片
|
||
|
}
|
||
|
|
||
|
func DataTransferHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
/*// 解析JWT token,并对空用户进行判断
|
||
|
claims, err := svcCtx.ParseJwtToken(r)
|
||
|
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||
|
if err != nil {
|
||
|
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
||
|
w.Write([]byte("connect failed:unauthorized"))
|
||
|
return
|
||
|
}
|
||
|
var userInfo *auth.UserInfo
|
||
|
if claims != nil {
|
||
|
// 从token中获取对应的用户信息
|
||
|
userInfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||
|
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
// 如果claims为nil,则认为用户身份为白板用户
|
||
|
w.Write([]byte("connect failed:unauthorized!!"))
|
||
|
return
|
||
|
}
|
||
|
//升级websocket
|
||
|
conn, err := upgrade.Upgrade(w, r, r.Header)
|
||
|
if err != nil {
|
||
|
logx.Error("http upgrade websocket err:", err)
|
||
|
w.Write([]byte("http upgrade websocket err"))
|
||
|
return
|
||
|
}*/
|
||
|
}
|
||
|
}
|