From 9e1889b9569fe0624e35c88496a722799f3bae8d Mon Sep 17 00:00:00 2001 From: "DESKTOP-V763RJ7\\Administrator" <835606593@qq.com> Date: Sun, 11 Jan 2026 16:16:16 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9usn=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=B8=BAstring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 6 ++---- internal/global/global.go | 1 + internal/grpc_server/server/server.go | 2 +- internal/grpc_server/stream_client/scene.go | 8 ++++---- internal/handler/http_handler/login.go | 6 +++--- internal/handler/ws_handler/client/client.go | 14 +++++++------- .../handler/ws_handler/client/client_handler.go | 4 ++-- internal/handler/ws_handler/client/manager.go | 14 +++++++------- internal/handler/ws_handler/login/login.go | 2 +- internal/handler/ws_handler/login/queue_up.go | 4 ++-- internal/net/http_gateway/middleward.go | 3 +-- internal/net/ws_gateway/server.go | 4 ++-- 13 files changed, 34 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 5cd8878..8d5660e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.23.1 require ( bou.ke/monkey v1.0.2 - git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77 + git.hlsq.asia/mmorpg/service-common v0.0.0-20260110081319-015e333b88c0 github.com/alicebob/miniredis/v2 v2.35.0 github.com/gin-contrib/cors v1.7.6 github.com/gin-gonic/gin v1.11.0 diff --git a/go.sum b/go.sum index e05059b..3c31d3a 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78 h1:4CP8w5jHDOdN7hym8YrtioxNDd7PS2WcZDrO6dtY344= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77 h1:39SVfV3+uysM25P+dSnJMvtJJI9l2aZ+/n/ooTdHqxk= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260110081319-015e333b88c0 h1:GWd3ipi7UveYBAxe+UnTplqiKxOt25NVC5CC634lbuI= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260110081319-015e333b88c0/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI= diff --git a/internal/global/global.go b/internal/global/global.go index 25afc27..eae3737 100644 --- a/internal/global/global.go +++ b/internal/global/global.go @@ -18,6 +18,7 @@ const ( // PublicPaths 不需要鉴权的接口,硬编码注册 var PublicPaths = []string{ "/user/info", + "/user/generate_question", } var ( diff --git a/internal/grpc_server/server/server.go b/internal/grpc_server/server/server.go index 566b0a2..8925ad2 100644 --- a/internal/grpc_server/server/server.go +++ b/internal/grpc_server/server/server.go @@ -24,7 +24,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error { if args, err := server.Recv(); err != nil { return } else { - if args.USN == -1 { + if args.USN == "" { //utils.WorkerPool(ws_handler.UserMgr.GetAllInterface(), func(task interface{}) { // client := task.(*ws_handler.Client) diff --git a/internal/grpc_server/stream_client/scene.go b/internal/grpc_server/stream_client/scene.go index dda305a..85ff472 100644 --- a/internal/grpc_server/stream_client/scene.go +++ b/internal/grpc_server/stream_client/scene.go @@ -23,7 +23,7 @@ type sceneStream struct { stream grpc.ClientStream } -func findSceneBySID(sid int64, fun SceneFun) (*sceneStream, error) { +func findSceneBySID(sid string, fun SceneFun) (*sceneStream, error) { key := sceneKey(sid, fun) if v, ok := sceneServer.Load(key); ok { @@ -54,7 +54,7 @@ func findSceneBySID(sid int64, fun SceneFun) (*sceneStream, error) { return ss, nil } -func SendMessageToScene(sid int64, fun SceneFun, msg proto.Message, re ...bool) error { +func SendMessageToScene(sid string, fun SceneFun, msg proto.Message, re ...bool) error { ss, err := findSceneBySID(sid, fun) if err != nil { return err @@ -79,6 +79,6 @@ func SendMessageToScene(sid int64, fun SceneFun, msg proto.Message, re ...bool) return nil } -func sceneKey(sid int64, fun SceneFun) string { - return strconv.FormatInt(sid, 10) + "-" + strconv.Itoa(int(fun)) +func sceneKey(sid string, fun SceneFun) string { + return sid + "-" + strconv.Itoa(int(fun)) } diff --git a/internal/handler/http_handler/login.go b/internal/handler/http_handler/login.go index 7310da2..b68815f 100644 --- a/internal/handler/http_handler/login.go +++ b/internal/handler/http_handler/login.go @@ -23,7 +23,7 @@ type LoginReq struct { } type LoginResp struct { - USN int64 `json:"usn"` + USN string `json:"usn"` Name string `json:"name"` AccessToken string `json:"accessToken"` RefreshToken string `json:"refreshToken"` @@ -97,7 +97,7 @@ func RefreshToken(c *gin.Context) { })) } -func genToken(ctx context.Context, usn int64) (string, string, error) { +func genToken(ctx context.Context, usn string) (string, string, error) { at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, 2*time.Hour) if err != nil { return "", "", err @@ -109,7 +109,7 @@ func genToken(ctx context.Context, usn int64) (string, string, error) { return at, rt, nil } -func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) { +func genTokenOne(ctx context.Context, key string, usn string, ttl time.Duration) (string, error) { token, err := utils.GenToken(usn, config.Get().Auth.Secret, time.Duration(config.Get().Auth.Expire)*time.Second) if err != nil { return "", err diff --git a/internal/handler/ws_handler/client/client.go b/internal/handler/ws_handler/client/client.go index 2173659..d3dde6f 100644 --- a/internal/handler/ws_handler/client/client.go +++ b/internal/handler/ws_handler/client/client.go @@ -13,7 +13,7 @@ import ( "time" ) -var GatewaySID int64 +var GatewaySID string type Client struct { sync.WaitGroup @@ -24,14 +24,14 @@ type Client struct { cancel context.CancelFunc // 取消上下文 heartBeat time.Time // 最后一次心跳 - Status int32 // 状态:0 登陆中 1 正常 2 离线 - USN int64 // 用户ID - SceneSID int64 // 场景服ID - InstanceID int32 // 副本ID,副本类型 - UniqueNo int64 // 副本唯一编号 + Status int32 // 状态:0 登陆中 1 正常 2 离线 + USN string // 用户ID + SceneSID string // 场景服ID + InstanceID int32 // 副本ID,副本类型 + UniqueNo string // 副本唯一编号 } -func NewClient(usn int64, conn socket.ISocketConn) *Client { +func NewClient(usn string, conn socket.ISocketConn) *Client { client := &Client{ USN: usn, conn: conn, diff --git a/internal/handler/ws_handler/client/client_handler.go b/internal/handler/ws_handler/client/client_handler.go index 28cfc98..f156153 100644 --- a/internal/handler/ws_handler/client/client_handler.go +++ b/internal/handler/ws_handler/client/client_handler.go @@ -76,7 +76,7 @@ func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) { } func (c *Client) onLeave() { - if c.SceneSID == 0 { + if c.SceneSID == "" { return } client, err := service.SceneNewClient(c.SceneSID) @@ -95,7 +95,7 @@ func (c *Client) onLeave() { } func (c *Client) onAction(msg *sc_pb.C2S_Action) { - if c.SceneSID == 0 { + if c.SceneSID == "" { return } if err := stream_client.SendMessageToScene(c.SceneSID, stream_client.FunAction, &grpc_pb.ActionReq{ diff --git a/internal/handler/ws_handler/client/manager.go b/internal/handler/ws_handler/client/manager.go index c46537c..3931e86 100644 --- a/internal/handler/ws_handler/client/manager.go +++ b/internal/handler/ws_handler/client/manager.go @@ -8,35 +8,35 @@ import ( var UserMgr *userManager type userManager struct { - userMap map[int64]*Client + userMap map[string]*Client sync.RWMutex } func init() { UserMgr = &userManager{ - userMap: make(map[int64]*Client), + userMap: make(map[string]*Client), } } -func (m *userManager) Add(usn int64, client *Client) { +func (m *userManager) Add(usn string, client *Client) { m.Lock() defer m.Unlock() m.userMap[usn] = client global.OnlineUsersGauge.Inc() } -func (m *userManager) Delete(usn int64) { +func (m *userManager) Delete(usn string) { m.Lock() defer m.Unlock() delete(m.userMap, usn) global.OnlineUsersGauge.Dec() } -func (m *userManager) GetAll() map[int64]*Client { +func (m *userManager) GetAll() map[string]*Client { m.RLock() defer m.RUnlock() - copyMap := make(map[int64]*Client, len(m.userMap)) + copyMap := make(map[string]*Client, len(m.userMap)) for k, v := range m.userMap { copyMap[k] = v } @@ -54,7 +54,7 @@ func (m *userManager) GetAllInterface() []interface{} { return r } -func (m *userManager) GetByUSN(usn int64) *Client { +func (m *userManager) GetByUSN(usn string) *Client { m.RLock() defer m.RUnlock() return m.userMap[usn] diff --git a/internal/handler/ws_handler/login/login.go b/internal/handler/ws_handler/login/login.go index 15dc46e..0e7e75d 100644 --- a/internal/handler/ws_handler/login/login.go +++ b/internal/handler/ws_handler/login/login.go @@ -161,7 +161,7 @@ func (l *Login) CheckOnline(user *User) string { } // KickUser 把玩家踢下线 -func (l *Login) KickUser(gatewaySID int64, usn int64) bool { +func (l *Login) KickUser(gatewaySID string, usn string) bool { gc, err := service.GatewayNewClient(gatewaySID) if err != nil { log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID) diff --git a/internal/handler/ws_handler/login/queue_up.go b/internal/handler/ws_handler/login/queue_up.go index e715ad1..eab2a88 100644 --- a/internal/handler/ws_handler/login/queue_up.go +++ b/internal/handler/ws_handler/login/queue_up.go @@ -78,7 +78,7 @@ func (q *QueueUp) Dequeue() (*client.Client, error) { } // GetPosition 返回用户前面还有多少人在排队 -func (q *QueueUp) GetPosition(usn int64) (int64, bool) { +func (q *QueueUp) GetPosition(usn string) (int64, bool) { val, ok := q.waiting.Load(usn) if !ok { return 0, false @@ -88,7 +88,7 @@ func (q *QueueUp) GetPosition(usn int64) (int64, bool) { } // RemoveUser 安全移除用户(标记为取消) -func (q *QueueUp) RemoveUser(usn int64) bool { +func (q *QueueUp) RemoveUser(usn string) bool { _, loaded := q.waiting.LoadAndDelete(usn) return loaded } diff --git a/internal/net/http_gateway/middleward.go b/internal/net/http_gateway/middleward.go index 8023040..cbc3d64 100644 --- a/internal/net/http_gateway/middleward.go +++ b/internal/net/http_gateway/middleward.go @@ -9,7 +9,6 @@ import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "go.uber.org/zap" - "strconv" "strings" "time" ) @@ -73,7 +72,7 @@ func authJwt() gin.HandlerFunc { } // 这里将Header写到请求中,grpc-gateway框架会读取然后传给grpc服务 - c.Request.Header.Set("X-Usn", strconv.Itoa(int(claims.USN))) + c.Request.Header.Set("X-Usn", claims.USN) c.Next() } } diff --git a/internal/net/ws_gateway/server.go b/internal/net/ws_gateway/server.go index 774c4d6..b69189f 100644 --- a/internal/net/ws_gateway/server.go +++ b/internal/net/ws_gateway/server.go @@ -44,7 +44,7 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action { func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action { cli, ok := conn.GetParam("client").(*client.Client) - if !ok || cli.USN == 0 || cli.Status != 1 { + if !ok || cli.USN == "" || cli.Status != 1 { return socket.Close } cli.OnEvent(&client.ClientEvent{Msg: bytes}) @@ -53,7 +53,7 @@ func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socke func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) { cli, ok := conn.GetParam("client").(*client.Client) - if !ok || cli.USN == 0 { + if !ok || cli.USN == "" { return } cli.OnEvent(&client.PongEvent{})