2023-07-24 16:07:05 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
2023-08-23 15:08:45 +08:00
|
|
|
|
//websocket连接
|
2023-07-24 16:07:05 +08:00
|
|
|
|
import (
|
2023-07-26 16:00:19 +08:00
|
|
|
|
"bytes"
|
2023-08-23 16:13:14 +08:00
|
|
|
|
"encoding/hex"
|
2023-07-26 11:53:06 +08:00
|
|
|
|
"encoding/json"
|
2023-08-25 12:21:02 +08:00
|
|
|
|
"errors"
|
2023-10-11 17:50:08 +08:00
|
|
|
|
"fmt"
|
2023-07-26 11:53:06 +08:00
|
|
|
|
"fusenapi/constants"
|
2023-10-11 18:54:14 +08:00
|
|
|
|
"fusenapi/server/websocket/internal/types"
|
2023-07-26 17:30:35 +08:00
|
|
|
|
"fusenapi/utils/auth"
|
2023-08-28 11:33:25 +08:00
|
|
|
|
"fusenapi/utils/basic"
|
2023-08-23 16:45:01 +08:00
|
|
|
|
"fusenapi/utils/encryption_decryption"
|
2023-08-24 14:24:04 +08:00
|
|
|
|
"fusenapi/utils/websocket_data"
|
2023-07-26 11:53:06 +08:00
|
|
|
|
"net/http"
|
2023-08-24 12:18:04 +08:00
|
|
|
|
"strings"
|
2023-07-26 11:53:06 +08:00
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
2023-07-24 16:07:05 +08:00
|
|
|
|
|
2023-08-14 14:12:33 +08:00
|
|
|
|
"github.com/google/uuid"
|
2023-08-08 14:20:57 +08:00
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
2023-07-24 16:07:05 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/websocket/internal/svc"
|
2023-08-08 14:20:57 +08:00
|
|
|
|
|
2023-07-24 16:07:05 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DataTransferLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DataTransferLogic {
|
|
|
|
|
return &DataTransferLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 11:53:06 +08:00
|
|
|
|
var (
|
2023-08-30 10:55:27 +08:00
|
|
|
|
//临时对象缓存池
|
2023-07-26 16:00:19 +08:00
|
|
|
|
buffPool = sync.Pool{
|
|
|
|
|
New: func() interface{} {
|
|
|
|
|
return bytes.Buffer{}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
//升级websocket
|
2023-08-23 12:29:59 +08:00
|
|
|
|
upgrader = websocket.Upgrader{
|
2023-08-24 12:24:24 +08:00
|
|
|
|
//最大可读取大小 1M
|
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
|
//最大可写大小 1M
|
|
|
|
|
WriteBufferSize: 1024,
|
2023-07-26 16:00:19 +08:00
|
|
|
|
//握手超时时间15s
|
|
|
|
|
HandshakeTimeout: time.Second * 15,
|
2023-07-26 11:53:06 +08:00
|
|
|
|
//允许跨域
|
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
|
return true
|
|
|
|
|
},
|
2023-08-28 15:50:18 +08:00
|
|
|
|
//写的缓冲队列
|
2023-07-27 19:30:40 +08:00
|
|
|
|
WriteBufferPool: &buffPool,
|
|
|
|
|
//是否支持压缩
|
2023-09-05 10:32:40 +08:00
|
|
|
|
EnableCompression: true,
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
2023-07-27 16:29:09 +08:00
|
|
|
|
//websocket连接存储
|
2023-07-26 11:53:06 +08:00
|
|
|
|
mapConnPool = sync.Map{}
|
2023-09-07 12:23:04 +08:00
|
|
|
|
//每个websocket连接入口缓冲队列长度默认值
|
2023-09-22 15:24:16 +08:00
|
|
|
|
websocketInChanLen = 2000
|
2023-09-07 12:23:04 +08:00
|
|
|
|
//每个websocket连接出口缓冲队列长度默认值
|
2023-09-22 15:24:16 +08:00
|
|
|
|
websocketOutChanLen = 2000
|
2023-07-26 11:53:06 +08:00
|
|
|
|
)
|
2023-07-24 16:07:05 +08:00
|
|
|
|
|
2023-07-26 17:06:53 +08:00
|
|
|
|
// 每个连接的连接基本属性
|
2023-07-26 11:53:06 +08:00
|
|
|
|
type wsConnectItem struct {
|
2023-11-06 09:54:21 +08:00
|
|
|
|
wid string //ws连接唯一标识(基本属性)
|
|
|
|
|
userId int64 //用户id(基本属性)
|
|
|
|
|
guestId int64 //游客id(基本属性)
|
2023-08-24 12:04:58 +08:00
|
|
|
|
conn *websocket.Conn //websocket的连接(基本属性)
|
2023-10-12 14:44:09 +08:00
|
|
|
|
connExpireTime int64 //websocket过期时间(跟随连接时候的token过期时间)
|
2023-08-30 11:25:20 +08:00
|
|
|
|
userAgent string //用户代理头信息(基本属性,用于重连标识验证因素之一)
|
2023-08-24 12:04:58 +08:00
|
|
|
|
logic *DataTransferLogic //logic(基本属性,用于获取上下文,配置或者操作数据库)
|
|
|
|
|
closeChan chan struct{} //ws连接关闭chan(基本属性)
|
|
|
|
|
isClose bool //是否已经关闭(基本属性)
|
2023-08-28 15:50:18 +08:00
|
|
|
|
inChan chan []byte //接受消息缓冲队列(基本属性)
|
|
|
|
|
outChan chan []byte //要发送回客户端的消息缓冲队列(基本属性)
|
2023-08-24 12:04:58 +08:00
|
|
|
|
mutex sync.Mutex //互斥锁(基本属性)
|
2023-10-18 11:03:07 +08:00
|
|
|
|
debug *auth.Debug //是否开启debug
|
2023-08-24 12:04:58 +08:00
|
|
|
|
extendRenderProperty extendRenderProperty //扩展云渲染属性(扩展属性)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
2023-07-24 16:07:05 +08:00
|
|
|
|
|
2023-08-18 16:21:45 +08:00
|
|
|
|
// 请求建立连接,升级websocket协议
|
2023-10-11 18:54:14 +08:00
|
|
|
|
func (l *DataTransferLogic) DataTransfer(req *types.DataTransferReq, w http.ResponseWriter, r *http.Request) {
|
2023-08-18 16:20:45 +08:00
|
|
|
|
//把子协议携带的token设置到标准token头信息中
|
2023-10-18 15:28:47 +08:00
|
|
|
|
tokens := r.Header.Get("Sec-Websocket-Protocol")
|
2023-10-11 18:54:14 +08:00
|
|
|
|
oldWid := req.Wid
|
2023-10-16 15:05:03 +08:00
|
|
|
|
oldWid = strings.Trim(oldWid, " ")
|
2023-08-24 11:38:42 +08:00
|
|
|
|
//有token是正常用户,无则是白板用户,也可以连接
|
2023-10-18 15:28:47 +08:00
|
|
|
|
if tokens != "" {
|
|
|
|
|
token := ""
|
|
|
|
|
debugToken := ""
|
|
|
|
|
tokenSlice := strings.Split(tokens, ",")
|
|
|
|
|
switch len(tokenSlice) {
|
|
|
|
|
case 1:
|
|
|
|
|
token = strings.Trim(tokenSlice[0], " ")
|
|
|
|
|
case 2:
|
|
|
|
|
token = strings.Trim(tokenSlice[0], " ")
|
|
|
|
|
debugToken = strings.Trim(tokenSlice[1], " ")
|
|
|
|
|
default:
|
|
|
|
|
logx.Error("invalid ws token:", tokens)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-18 16:05:18 +08:00
|
|
|
|
if token != "empty_token" && token != "" {
|
|
|
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
|
}
|
|
|
|
|
if debugToken != "empty_debug_token" && debugToken != "" {
|
|
|
|
|
r.Header.Set("Debug-Token", debugToken)
|
|
|
|
|
}
|
2023-08-24 11:43:16 +08:00
|
|
|
|
//设置Sec-Websocket-Protocol
|
2023-10-18 16:08:53 +08:00
|
|
|
|
upgrader.Subprotocols = strings.Split(tokens, ",")
|
2023-08-24 11:38:42 +08:00
|
|
|
|
}
|
2023-08-24 12:18:04 +08:00
|
|
|
|
//判断下是否火狐浏览器(获取浏览器第一条消息返回有收不到的bug需要延迟1秒)
|
|
|
|
|
userAgent := r.Header.Get("User-Agent")
|
|
|
|
|
//是否火狐浏览器
|
|
|
|
|
isFirefoxBrowser := false
|
|
|
|
|
if strings.Contains(userAgent, "Firefox") {
|
|
|
|
|
isFirefoxBrowser = true
|
|
|
|
|
}
|
2023-07-26 11:53:06 +08:00
|
|
|
|
//升级websocket
|
2023-08-23 12:29:59 +08:00
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("http upgrade websocket err:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-12 12:15:41 +08:00
|
|
|
|
//支持写压缩
|
|
|
|
|
conn.EnableWriteCompression(true)
|
2023-08-10 15:21:28 +08:00
|
|
|
|
//鉴权不成功后断开
|
2023-08-18 16:03:29 +08:00
|
|
|
|
var (
|
2023-08-07 19:13:16 +08:00
|
|
|
|
userInfo *auth.UserInfo
|
|
|
|
|
isAuth bool
|
|
|
|
|
)
|
2023-08-16 14:16:33 +08:00
|
|
|
|
isAuth, userInfo = l.checkAuth(r)
|
2023-07-27 15:46:07 +08:00
|
|
|
|
if !isAuth {
|
2023-08-18 17:33:42 +08:00
|
|
|
|
//未授权响应消息
|
2023-10-12 11:05:05 +08:00
|
|
|
|
l.unAuthResponse(conn, isFirefoxBrowser, "unAuth")
|
2023-08-23 18:34:01 +08:00
|
|
|
|
conn.Close()
|
2023-07-27 15:46:07 +08:00
|
|
|
|
return
|
2023-08-18 16:03:29 +08:00
|
|
|
|
}
|
2023-08-11 10:49:29 +08:00
|
|
|
|
//设置连接
|
2023-10-11 17:50:08 +08:00
|
|
|
|
ws, err := l.setConnPool(conn, userInfo, isFirefoxBrowser, userAgent, oldWid)
|
2023-08-23 16:57:18 +08:00
|
|
|
|
if err != nil {
|
2023-08-23 18:34:01 +08:00
|
|
|
|
conn.Close()
|
2023-08-23 16:57:18 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-08-10 19:41:25 +08:00
|
|
|
|
//循环读客户端信息
|
2023-09-05 14:39:35 +08:00
|
|
|
|
go ws.reciveBrowserMessage()
|
2023-08-30 11:13:10 +08:00
|
|
|
|
//消费出口数据并发送浏览器端
|
|
|
|
|
go ws.consumeOutChanData()
|
|
|
|
|
//消费入口数据
|
|
|
|
|
go ws.consumeInChanData()
|
2023-08-16 16:20:16 +08:00
|
|
|
|
//消费渲染缓冲队列
|
2023-08-30 11:13:10 +08:00
|
|
|
|
go ws.consumeRenderImageData()
|
2023-08-10 19:41:25 +08:00
|
|
|
|
//心跳
|
|
|
|
|
ws.heartbeat()
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 10:49:29 +08:00
|
|
|
|
// 设置连接
|
2023-10-11 17:50:08 +08:00
|
|
|
|
func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.UserInfo, isFirefoxBrowser bool, userAgent, oldWid string) (wsConnectItem, error) {
|
2023-08-25 12:21:02 +08:00
|
|
|
|
//生成连接唯一标识(失败重试10次)
|
2023-11-02 11:32:04 +08:00
|
|
|
|
wid, err := l.getUniqueId(userInfo, userAgent, 10)
|
2023-08-23 16:57:18 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
//发送获取唯一标识失败的消息
|
2023-10-18 17:18:25 +08:00
|
|
|
|
if isFirefoxBrowser {
|
|
|
|
|
time.Sleep(time.Second * 1) //兼容下火狐(直接发回去收不到第一条消息:有待研究)
|
|
|
|
|
}
|
2023-10-18 17:39:31 +08:00
|
|
|
|
l.sendGetUniqueIdErrResponse(conn, userInfo.Debug)
|
2023-08-23 16:57:18 +08:00
|
|
|
|
return wsConnectItem{}, err
|
|
|
|
|
}
|
2023-10-30 18:31:14 +08:00
|
|
|
|
//传入绑定的wid判断是否可重用(白板用户不可重用)
|
2023-11-06 10:12:42 +08:00
|
|
|
|
if oldWid != "" && (userInfo.IsUser() || userInfo.IsGuest()) {
|
2023-10-12 11:05:05 +08:00
|
|
|
|
for i := 0; i < 1; i++ {
|
|
|
|
|
//解析传入的wid是不是属于自己的用户的
|
|
|
|
|
decryptionWid, err := encryption_decryption.CBCDecrypt(oldWid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("解密wid失败:", err)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
lendecryptionWid := len(decryptionWid)
|
|
|
|
|
//合成client后缀,不是同个后缀的不能复用
|
2023-11-06 10:14:11 +08:00
|
|
|
|
userPart := getUserPart(userInfo.UserId, userInfo.GuestId, userAgent)
|
2023-10-12 11:05:05 +08:00
|
|
|
|
lenUserPart := len(userPart)
|
|
|
|
|
//长度太短
|
|
|
|
|
if lendecryptionWid <= lenUserPart {
|
|
|
|
|
logx.Error("复用的连接标识太短,不符合重用条件")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
//尾部不同不能复用
|
|
|
|
|
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
|
|
|
|
|
logx.Error("尾部用户信息不同,不符合重用条件")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
//存在是不能给他申请重新绑定
|
|
|
|
|
if _, ok := mapConnPool.Load(oldWid); ok {
|
2023-10-20 14:43:37 +08:00
|
|
|
|
logx.Error("复用的连接标识已被其他客户端使用,不符合重用条件,用户id:", userInfo.UserId, " guest_id:", userInfo.GuestId)
|
2023-10-12 11:05:05 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
2023-10-11 17:54:46 +08:00
|
|
|
|
logx.Info("====复用旧的ws连接成功====")
|
2023-11-02 11:32:04 +08:00
|
|
|
|
wid = oldWid
|
2023-10-11 17:54:46 +08:00
|
|
|
|
}
|
2023-10-11 17:50:08 +08:00
|
|
|
|
}
|
2023-10-12 14:44:09 +08:00
|
|
|
|
//默认过期时间
|
|
|
|
|
connExpireTime := time.Now().UTC().Add(time.Second * time.Duration(l.svcCtx.Config.Auth.AccessExpire)).Unix()
|
|
|
|
|
if userInfo.Exp > 0 {
|
|
|
|
|
connExpireTime = userInfo.Exp
|
|
|
|
|
}
|
2023-09-27 18:35:20 +08:00
|
|
|
|
renderCtx, renderCtxCancelFunc := context.WithCancel(l.ctx)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
ws := wsConnectItem{
|
2023-10-12 14:44:09 +08:00
|
|
|
|
conn: conn,
|
|
|
|
|
connExpireTime: connExpireTime,
|
|
|
|
|
userAgent: userAgent,
|
|
|
|
|
logic: l,
|
|
|
|
|
closeChan: make(chan struct{}, 1),
|
|
|
|
|
isClose: false,
|
2023-11-02 11:32:04 +08:00
|
|
|
|
wid: wid,
|
2023-10-12 14:44:09 +08:00
|
|
|
|
inChan: make(chan []byte, websocketInChanLen),
|
|
|
|
|
outChan: make(chan []byte, websocketOutChanLen),
|
|
|
|
|
mutex: sync.Mutex{},
|
|
|
|
|
userId: userInfo.UserId,
|
|
|
|
|
guestId: userInfo.GuestId,
|
2023-08-24 12:04:58 +08:00
|
|
|
|
extendRenderProperty: extendRenderProperty{
|
2023-09-27 18:35:20 +08:00
|
|
|
|
renderChan: make(chan websocket_data.RenderImageReqMsg, renderChanLen),
|
|
|
|
|
renderCtx: renderCtx,
|
|
|
|
|
renderCtxCancelFunc: renderCtxCancelFunc,
|
2023-07-26 17:06:53 +08:00
|
|
|
|
},
|
2023-10-18 11:03:07 +08:00
|
|
|
|
debug: userInfo.Debug,
|
|
|
|
|
}
|
2023-10-19 10:00:19 +08:00
|
|
|
|
//********强制开启debug 后面删掉
|
2023-10-31 15:23:01 +08:00
|
|
|
|
/*e := int64(1700359131)
|
2023-10-19 10:00:19 +08:00
|
|
|
|
ws.debug = &auth.Debug{
|
|
|
|
|
Exp: &e,
|
|
|
|
|
IsCache: 1,
|
|
|
|
|
IsAllTemplateTag: 0,
|
2023-10-31 15:23:01 +08:00
|
|
|
|
}*/
|
2023-07-26 11:53:06 +08:00
|
|
|
|
//保存连接
|
2023-11-02 11:32:04 +08:00
|
|
|
|
mapConnPool.Store(wid, ws)
|
2023-10-31 17:38:21 +08:00
|
|
|
|
//累加统计连接数
|
|
|
|
|
increaseWebsocketConnectCount(userInfo.UserId, userInfo.GuestId)
|
2023-09-04 11:17:12 +08:00
|
|
|
|
//非白板用户,需要为这个用户建立map索引便于通过用户查询
|
2023-11-02 11:32:04 +08:00
|
|
|
|
createUserConnPoolElement(userInfo.UserId, userInfo.GuestId, wid)
|
2023-08-24 12:18:04 +08:00
|
|
|
|
if isFirefoxBrowser {
|
2023-08-18 16:23:04 +08:00
|
|
|
|
time.Sleep(time.Second * 1) //兼容下火狐(直接发回去收不到第一条消息:有待研究)
|
2023-08-24 12:18:04 +08:00
|
|
|
|
}
|
2023-11-02 11:32:04 +08:00
|
|
|
|
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, websocket_data.ConnectSuccessMsg{Wid: wid}))
|
2023-08-23 16:57:18 +08:00
|
|
|
|
return ws, nil
|
2023-08-10 19:41:25 +08:00
|
|
|
|
}
|
2023-08-11 10:49:29 +08:00
|
|
|
|
|
|
|
|
|
// 获取唯一id
|
2023-08-30 11:25:20 +08:00
|
|
|
|
func (l *DataTransferLogic) getUniqueId(userInfo *auth.UserInfo, userAgent string, retryTimes int) (uniqueId string, err error) {
|
2023-08-25 12:21:02 +08:00
|
|
|
|
if retryTimes < 0 {
|
|
|
|
|
return "", errors.New("failed to get unique id")
|
|
|
|
|
}
|
2023-08-15 18:38:33 +08:00
|
|
|
|
//后面拼接上用户id
|
2023-11-06 10:14:11 +08:00
|
|
|
|
uniqueId = hex.EncodeToString([]byte(uuid.New().String())) + getUserPart(userInfo.UserId, userInfo.GuestId, userAgent)
|
2023-08-25 12:21:02 +08:00
|
|
|
|
//存在则从新获取
|
2023-08-10 19:41:25 +08:00
|
|
|
|
if _, ok := mapConnPool.Load(uniqueId); ok {
|
2023-08-30 11:25:20 +08:00
|
|
|
|
uniqueId, err = l.getUniqueId(userInfo, userAgent, retryTimes-1)
|
2023-08-23 16:45:01 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//加密
|
|
|
|
|
uniqueId, err = encryption_decryption.CBCEncrypt(uniqueId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2023-08-10 19:41:25 +08:00
|
|
|
|
}
|
2023-10-12 11:19:01 +08:00
|
|
|
|
return uniqueId, nil
|
2023-07-24 16:07:05 +08:00
|
|
|
|
}
|
2023-07-26 11:53:06 +08:00
|
|
|
|
|
2023-07-26 17:30:35 +08:00
|
|
|
|
// 鉴权
|
2023-08-16 14:16:33 +08:00
|
|
|
|
func (l *DataTransferLogic) checkAuth(r *http.Request) (isAuth bool, userInfo *auth.UserInfo) {
|
2023-07-26 17:30:35 +08:00
|
|
|
|
// 解析JWT token,并对空用户进行判断
|
2023-08-28 11:33:25 +08:00
|
|
|
|
userInfo, err := basic.ParseJwtToken(r, l.svcCtx)
|
2023-07-26 17:30:35 +08:00
|
|
|
|
if err != nil {
|
2023-10-18 15:38:26 +08:00
|
|
|
|
logx.Error("未授权:", err.Error())
|
2023-07-27 12:32:03 +08:00
|
|
|
|
return false, nil
|
2023-07-26 17:30:35 +08:00
|
|
|
|
}
|
2023-09-05 10:00:01 +08:00
|
|
|
|
if userInfo.UserId > 0 {
|
|
|
|
|
userInfo.GuestId = 0
|
|
|
|
|
}
|
2023-08-24 11:27:53 +08:00
|
|
|
|
//白板用户
|
2023-08-28 11:33:25 +08:00
|
|
|
|
return true, userInfo
|
2023-07-26 17:30:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 18:31:25 +08:00
|
|
|
|
// 心跳检测
|
2023-07-26 11:53:06 +08:00
|
|
|
|
func (w *wsConnectItem) heartbeat() {
|
2023-07-27 10:08:30 +08:00
|
|
|
|
tick := time.Tick(time.Second * 5)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan:
|
|
|
|
|
return
|
2023-07-26 18:00:46 +08:00
|
|
|
|
case <-tick:
|
2023-10-12 14:44:09 +08:00
|
|
|
|
//看看token是否过期了
|
|
|
|
|
if w.connExpireTime > 0 && w.connExpireTime < time.Now().UTC().Unix() {
|
2023-11-02 11:32:04 +08:00
|
|
|
|
logx.Info("token过期,关闭连接:", w.wid)
|
2023-10-12 14:44:09 +08:00
|
|
|
|
w.close()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-18 15:10:45 +08:00
|
|
|
|
//查看debug的时间是否过期
|
|
|
|
|
if w.debug != nil && w.debug.Exp != nil && *w.debug.Exp < time.Now().UTC().Unix() {
|
|
|
|
|
w.debug = nil
|
|
|
|
|
}
|
2023-10-20 15:14:41 +08:00
|
|
|
|
if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil {
|
2023-11-02 11:32:04 +08:00
|
|
|
|
logx.Error("发送心跳信息异常,关闭连接:", w.wid, err)
|
2023-08-24 11:56:10 +08:00
|
|
|
|
w.close()
|
2023-07-26 16:23:38 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 18:31:25 +08:00
|
|
|
|
// 关闭websocket连接
|
2023-07-26 11:53:06 +08:00
|
|
|
|
func (w *wsConnectItem) close() {
|
|
|
|
|
w.mutex.Lock()
|
|
|
|
|
defer w.mutex.Unlock()
|
2023-11-02 11:32:04 +08:00
|
|
|
|
logx.Info("###websocket:", w.wid, " uid:", w.userId, " gid:", w.guestId, " is closing....")
|
2023-07-26 17:39:56 +08:00
|
|
|
|
//发送关闭信息
|
|
|
|
|
_ = w.conn.WriteMessage(websocket.CloseMessage, nil)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
w.conn.Close()
|
2023-11-02 11:32:04 +08:00
|
|
|
|
mapConnPool.Delete(w.wid)
|
2023-07-26 11:53:06 +08:00
|
|
|
|
if !w.isClose {
|
|
|
|
|
w.isClose = true
|
|
|
|
|
close(w.closeChan)
|
2023-09-04 11:17:12 +08:00
|
|
|
|
//删除用户级索引
|
2023-11-02 11:32:04 +08:00
|
|
|
|
deleteUserConnPoolElement(w.userId, w.guestId, w.wid)
|
2023-09-07 15:54:24 +08:00
|
|
|
|
//减少连接数统计
|
2023-10-31 14:23:08 +08:00
|
|
|
|
decreaseWebsocketConnectCount(w.userId, w.guestId)
|
2023-11-02 11:32:04 +08:00
|
|
|
|
//通知unity取消任务
|
|
|
|
|
sendCancelRenderMsgToUnity(w.wid, time.Now().UTC().Unix())
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
2023-11-02 11:32:04 +08:00
|
|
|
|
logx.Info("###websocket:", w.wid, " uid:", w.userId, " gid:", w.guestId, " is closed")
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 15:50:18 +08:00
|
|
|
|
// 读取出口缓冲队列数据输出返回给浏览器端
|
2023-08-30 11:13:10 +08:00
|
|
|
|
func (w *wsConnectItem) consumeOutChanData() {
|
2023-08-22 18:37:49 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2023-09-04 11:17:12 +08:00
|
|
|
|
logx.Error("consumeOutChanData panic:", err)
|
2023-08-22 18:37:49 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|
2023-07-26 11:53:06 +08:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan: //如果关闭了
|
|
|
|
|
return
|
|
|
|
|
case data := <-w.outChan:
|
2023-07-26 18:03:27 +08:00
|
|
|
|
if err := w.conn.WriteMessage(websocket.TextMessage, data); err != nil {
|
|
|
|
|
logx.Error("websocket write loop err:", err)
|
|
|
|
|
w.close()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 11:13:10 +08:00
|
|
|
|
// 消费websocket入口数据池中的数据
|
|
|
|
|
func (w *wsConnectItem) consumeInChanData() {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2023-09-04 11:17:12 +08:00
|
|
|
|
logx.Error("consumeInChanData:", err)
|
2023-08-30 11:13:10 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan:
|
|
|
|
|
return
|
|
|
|
|
case data := <-w.inChan:
|
2023-08-30 17:07:14 +08:00
|
|
|
|
//对不同消息类型分发处理
|
|
|
|
|
w.allocationProcessing(data)
|
2023-08-30 11:13:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 接受浏览器端发来的消息并写入入口缓冲队列
|
2023-09-05 14:39:35 +08:00
|
|
|
|
func (w *wsConnectItem) reciveBrowserMessage() {
|
2023-08-22 18:37:49 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2023-09-04 11:17:12 +08:00
|
|
|
|
logx.Error("acceptBrowserMessage panic:", err)
|
2023-08-22 18:37:49 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|
2023-07-26 11:53:06 +08:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan: //如果关闭了
|
|
|
|
|
return
|
2023-08-28 15:34:30 +08:00
|
|
|
|
default: //收取消息
|
2023-07-26 17:55:14 +08:00
|
|
|
|
msgType, data, err := w.conn.ReadMessage()
|
2023-07-26 16:23:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error("接受信息错误:", err)
|
|
|
|
|
//关闭连接
|
|
|
|
|
w.close()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-05 14:45:15 +08:00
|
|
|
|
switch msgType {
|
|
|
|
|
case websocket.PingMessage, websocket.PongMessage: //心跳消息(过滤不处理)
|
|
|
|
|
continue
|
|
|
|
|
case websocket.BinaryMessage, websocket.TextMessage: //主要消息
|
2023-08-28 15:47:25 +08:00
|
|
|
|
w.sendToInChan(data)
|
2023-09-05 14:45:15 +08:00
|
|
|
|
case websocket.CloseMessage: //客户端主动关闭消息
|
|
|
|
|
w.close()
|
2023-07-26 17:55:14 +08:00
|
|
|
|
}
|
2023-07-26 11:53:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 15:50:18 +08:00
|
|
|
|
// 把要传递给客户端的数据放入出口缓冲队列
|
2023-07-26 16:58:14 +08:00
|
|
|
|
func (w *wsConnectItem) sendToOutChan(data []byte) {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan:
|
|
|
|
|
return
|
|
|
|
|
case w.outChan <- data:
|
2023-08-15 17:40:27 +08:00
|
|
|
|
return
|
2023-08-28 15:47:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送接受到的消息到入口缓冲队列中
|
|
|
|
|
func (w *wsConnectItem) sendToInChan(data []byte) {
|
|
|
|
|
select {
|
|
|
|
|
case <-w.closeChan: //关闭了
|
|
|
|
|
return
|
|
|
|
|
case w.inChan <- data:
|
|
|
|
|
return
|
2023-07-26 12:27:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 18:31:25 +08:00
|
|
|
|
// 格式化为websocket标准返回格式
|
2023-08-23 15:08:45 +08:00
|
|
|
|
func (w *wsConnectItem) respondDataFormat(msgType constants.Websocket, data interface{}) []byte {
|
2023-08-07 11:25:06 +08:00
|
|
|
|
d := websocket_data.DataTransferData{
|
2023-10-18 17:39:31 +08:00
|
|
|
|
T: msgType,
|
|
|
|
|
D: data,
|
|
|
|
|
Debug: w.debug != nil,
|
2023-07-27 17:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
b, _ := json.Marshal(d)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 17:50:08 +08:00
|
|
|
|
// 获取用户拼接部分(复用标识用到)
|
2023-11-06 10:14:11 +08:00
|
|
|
|
func getUserPart(userId, guestId int64, userAgent string) string {
|
2023-10-11 17:50:08 +08:00
|
|
|
|
if userId > 0 {
|
|
|
|
|
guestId = 0
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("|_%d_%d_|_%s_|", userId, guestId, userAgent)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 15:50:18 +08:00
|
|
|
|
// 处理入口缓冲队列中不同类型的数据(分发处理)
|
2023-08-30 17:07:14 +08:00
|
|
|
|
func (w *wsConnectItem) allocationProcessing(data []byte) {
|
2023-08-07 11:25:06 +08:00
|
|
|
|
var parseInfo websocket_data.DataTransferData
|
2023-07-26 15:08:43 +08:00
|
|
|
|
if err := json.Unmarshal(data, &parseInfo); err != nil {
|
2023-08-24 17:23:41 +08:00
|
|
|
|
w.incomeDataFormatErrResponse("invalid format of income message:" + string(data))
|
2023-08-15 18:14:44 +08:00
|
|
|
|
logx.Error("invalid format of websocket message:", err)
|
2023-07-26 15:08:43 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-26 16:47:53 +08:00
|
|
|
|
d, _ := json.Marshal(parseInfo.D)
|
2023-08-30 17:40:40 +08:00
|
|
|
|
//获取工厂实例
|
|
|
|
|
processor := w.newAllocationProcessor(parseInfo.T)
|
2023-08-30 18:31:20 +08:00
|
|
|
|
if processor == nil {
|
2023-10-17 17:49:49 +08:00
|
|
|
|
//logx.Error("未知消息类型:", string(data))
|
2023-08-30 18:31:20 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-08-30 17:40:40 +08:00
|
|
|
|
//执行工厂方法
|
2023-08-30 18:31:20 +08:00
|
|
|
|
processor.allocationMessage(w, d)
|
2023-07-26 15:08:43 +08:00
|
|
|
|
}
|