feat(common): 重构数据包处理逻辑并添加全局 ID 生成器
- 引入 idgenerator-go 库,实现全局唯一 ID 生成 - 重构 Pack 函数,使用接口参数提高灵活性 - 修改 Player 结构,增加 MainConn 字段用于主连接 - 更新 SocketHandler_Tomee 中的 Data 字段标记 - 优化 Recv 函数中的数据解包和参数处理逻辑
This commit is contained in:
@@ -4,11 +4,7 @@ import (
|
||||
baseservice "blazing/modules/base/service"
|
||||
"blazing/modules/blazing/service"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@@ -48,27 +44,16 @@ func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (re
|
||||
}
|
||||
|
||||
accountID := res1.ID
|
||||
retsid, sid, err := service.NewLoginServiceService().GetSessionId(accountID)
|
||||
if err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
|
||||
// 生成SID
|
||||
sidInfo := fmt.Sprintf("%d%d", accountID, time.Now().UnixNano())
|
||||
hash := sha256.Sum256([]byte(sidInfo))
|
||||
sidByte := hex.EncodeToString(hash[:])
|
||||
|
||||
// 确保长度为48字符
|
||||
if len(sidByte) < 48 {
|
||||
sidByte = sidByte + strings.Repeat("0", 48-len(sidByte))
|
||||
} else {
|
||||
sidByte = sidByte[:48]
|
||||
}
|
||||
|
||||
// UID拼接SID
|
||||
hex8 := fmt.Sprintf("%08X", int(accountID&0xFFFFFFFF))
|
||||
res.Session = hex8 + sidByte[8:]
|
||||
res.Session = retsid
|
||||
|
||||
// 保存后端校验的SID
|
||||
backendSID := res.Session[8 : len(res.Session)-8]
|
||||
fmt.Println("存储sid", backendSID)
|
||||
if err := service.NewLoginServiceService().SaveSessionId(backendSID, gconv.String(accountID)); err != nil {
|
||||
if err := service.NewLoginServiceService().SaveSessionId(sid, gconv.String(accountID)); err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user