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