feat 包命名规范
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
"git.hlsq.asia/mmorpg/service-common/module"
|
"git.hlsq.asia/mmorpg/service-common/module"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/grpcserver"
|
||||||
"github.com/judwhite/go-svc"
|
"github.com/judwhite/go-svc"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -23,7 +23,7 @@ func (p *Program) Init(_ svc.Environment) error {
|
|||||||
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
||||||
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpc_server.NewServer(config.Get().Serve.Grpc)})
|
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpcserver.NewServer(config.Get().Serve.Grpc)})
|
||||||
p.moduleList = append(p.moduleList, &module.Prometheus{Cfg: config.Get().Metric})
|
p.moduleList = append(p.moduleList, &module.Prometheus{Cfg: config.Get().Metric})
|
||||||
p.moduleList = append(p.moduleList, &module.Tracer{Cfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameGateway})
|
p.moduleList = append(p.moduleList, &module.Tracer{Cfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameGateway})
|
||||||
p.moduleList = append(p.moduleList, &module.Discover{})
|
p.moduleList = append(p.moduleList, &module.Discover{})
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/module"
|
"git.hlsq.asia/mmorpg/service-common/module"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/http_gateway"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/net/httpgateway"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -30,7 +30,7 @@ func (m *ModuleWebServer) Start(ready *sync.WaitGroup) error {
|
|||||||
defer m.wg.Done()
|
defer m.wg.Done()
|
||||||
m.server = &http.Server{
|
m.server = &http.Server{
|
||||||
Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port),
|
Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port),
|
||||||
Handler: http_gateway.InitRouter(),
|
Handler: httpgateway.InitRouter(),
|
||||||
}
|
}
|
||||||
ready.Done()
|
ready.Done()
|
||||||
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"git.hlsq.asia/mmorpg/service-common/module"
|
"git.hlsq.asia/mmorpg/service-common/module"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/socket/websocket"
|
"git.hlsq.asia/mmorpg/service-common/net/socket/websocket"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/ws_gateway"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/net/wsgateway"
|
||||||
"github.com/panjf2000/gnet/v2"
|
"github.com/panjf2000/gnet/v2"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -22,7 +22,7 @@ type ModuleWebsocketServer struct {
|
|||||||
func (m *ModuleWebsocketServer) Init() error {
|
func (m *ModuleWebsocketServer) Init() error {
|
||||||
m.wg = &sync.WaitGroup{}
|
m.wg = &sync.WaitGroup{}
|
||||||
m.server = websocket.NewWSServer(
|
m.server = websocket.NewWSServer(
|
||||||
&ws_gateway.GatewayWsServer{},
|
&wsgateway.GatewayWsServer{},
|
||||||
log.GetLogger().Named("ws_server"),
|
log.GetLogger().Named("ws_server"),
|
||||||
5*time.Second,
|
5*time.Second,
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.24.0
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
bou.ke/monkey v1.0.2
|
bou.ke/monkey v1.0.2
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260211064821-558677bc3a2e
|
||||||
github.com/alicebob/miniredis/v2 v2.35.0
|
github.com/alicebob/miniredis/v2 v2.35.0
|
||||||
github.com/gin-contrib/cors v1.7.6
|
github.com/gin-contrib/cors v1.7.6
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -4,6 +4,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
|||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14 h1:4r3tNWRzGVY3Xx6UiGjJJnwoMoWlVqbyGrljxl5d/nQ=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14 h1:4r3tNWRzGVY3Xx6UiGjJJnwoMoWlVqbyGrljxl5d/nQ=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||||
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260211064821-558677bc3a2e h1:MNVS1NXNVn4DeTZPcvvA8YFM8afCzZedLPUYMffNgZs=
|
||||||
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260211064821-558677bc3a2e/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package grpc_server
|
package grpcserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/sspb"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/wshandler/client"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
func (s *Server) ToClient(server rspb.Gateway_ToClientServer) error {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -32,8 +32,8 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
|||||||
// client.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
|
// client.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
|
||||||
//})
|
//})
|
||||||
|
|
||||||
data, err := proto.Marshal(&ss_pb.Message{
|
data, err := proto.Marshal(&sspb.Message{
|
||||||
ID: ss_pb.MessageID(args.MessageID),
|
ID: sspb.MessageID(args.MessageID),
|
||||||
Payload: args.Payload,
|
Payload: args.Payload,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,7 +51,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
|||||||
//}
|
//}
|
||||||
} else {
|
} else {
|
||||||
if cli := client.UserMgr.GetByUSN(args.USN); cli != nil {
|
if cli := client.UserMgr.GetByUSN(args.USN); cli != nil {
|
||||||
cli.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
|
cli.WriteBytes(sspb.MessageID(args.MessageID), args.Payload)
|
||||||
global.FlowCounter.Add(float64(len(args.Payload)))
|
global.FlowCounter.Add(float64(len(args.Payload)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,12 +59,12 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return server.SendAndClose(&grpc_pb.ToClientResp{})
|
return server.SendAndClose(&rspb.ToClientResp{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) KickUser(ctx context.Context, req *grpc_pb.KickUserReq) (*grpc_pb.KickUserResp, error) {
|
func (s *Server) KickUser(ctx context.Context, req *rspb.KickUserReq) (*rspb.KickUserResp, error) {
|
||||||
if cli := client.UserMgr.GetByUSN(req.USN); cli != nil {
|
if cli := client.UserMgr.GetByUSN(req.USN); cli != nil {
|
||||||
cli.CloseClient()
|
cli.CloseClient()
|
||||||
}
|
}
|
||||||
return &grpc_pb.KickUserResp{}, nil
|
return &rspb.KickUserResp{}, nil
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
package grpc_server
|
package grpcserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.hlsq.asia/mmorpg/service-common/config"
|
"git.hlsq.asia/mmorpg/service-common/config"
|
||||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
grpc_pb.UnimplementedGatewayServer
|
rspb.UnimplementedGatewayServer
|
||||||
service.Base
|
service.Base
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
|
|||||||
|
|
||||||
func (s *Server) OnInit(serve *grpc.Server) {
|
func (s *Server) OnInit(serve *grpc.Server) {
|
||||||
global.GatewaySID = s.SID
|
global.GatewaySID = s.SID
|
||||||
grpc_pb.RegisterGatewayServer(serve, s)
|
rspb.RegisterGatewayServer(serve, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) OnClose() {
|
func (s *Server) OnClose() {
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package http_handler
|
package httphandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -29,51 +29,51 @@ type LoginResp struct {
|
|||||||
func Login(c *gin.Context) {
|
func Login(c *gin.Context) {
|
||||||
req := &LoginReq{}
|
req := &LoginReq{}
|
||||||
if err := c.ShouldBindJSON(req); err != nil {
|
if err := c.ShouldBindJSON(req); err != nil {
|
||||||
http_resp.JsonBadRequest(c)
|
httpresp.JsonBadRequest(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client, err := grpc_client.UserNewClientLB()
|
client, err := grpcclient.UserNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Login UserNewClientLB error: %v", err)
|
log.Errorf("Login UserNewClientLB error: %v", err)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
usn, name := int64(0), ""
|
usn, name := int64(0), ""
|
||||||
if req.Phone != "" {
|
if req.Phone != "" {
|
||||||
// TODO 校验验证码
|
// TODO 校验验证码
|
||||||
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
|
login, err := client.PhoneLogin(c, &rspb.PhoneLoginReq{
|
||||||
Phone: req.Phone,
|
Phone: req.Phone,
|
||||||
Code: req.Code,
|
Code: req.Code,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Login PhoneLogin error: %v", err)
|
log.Errorf("Login PhoneLogin error: %v", err)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
usn, name = login.USN, login.Name
|
usn, name = login.USN, login.Name
|
||||||
} else if req.WxMiniCode != "" {
|
} else if req.WxMiniCode != "" {
|
||||||
login, err := client.WxMiniLogin(c, &grpc_pb.WxMiniLoginReq{
|
login, err := client.WxMiniLogin(c, &rspb.WxMiniLoginReq{
|
||||||
Code: req.WxMiniCode,
|
Code: req.WxMiniCode,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Login WxMiniLogin error: %v", err)
|
log.Errorf("Login WxMiniLogin error: %v", err)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
usn, name = login.USN, login.Name
|
usn, name = login.USN, login.Name
|
||||||
} else {
|
} else {
|
||||||
http_resp.JsonBadRequest(c)
|
httpresp.JsonBadRequest(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
at, rt, err := sessionLogin(c, usn)
|
at, rt, err := sessionLogin(c, usn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn)
|
log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http_resp.JsonOK(c, http_resp.Success(&LoginResp{
|
httpresp.JsonOK(c, httpresp.Success(&LoginResp{
|
||||||
USN: usn,
|
USN: usn,
|
||||||
Name: name,
|
Name: name,
|
||||||
AccessToken: at,
|
AccessToken: at,
|
||||||
@@ -93,13 +93,13 @@ type RefreshTokenResp struct {
|
|||||||
func RefreshToken(c *gin.Context) {
|
func RefreshToken(c *gin.Context) {
|
||||||
req := &RefreshTokenReq{}
|
req := &RefreshTokenReq{}
|
||||||
if err := c.ShouldBindJSON(req); err != nil {
|
if err := c.ShouldBindJSON(req); err != nil {
|
||||||
http_resp.JsonBadRequest(c)
|
httpresp.JsonBadRequest(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.RefreshToken == "" {
|
if req.RefreshToken == "" {
|
||||||
cookie, err := c.Cookie("refresh_token")
|
cookie, err := c.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http_resp.JsonUnauthorized(c)
|
httpresp.JsonUnauthorized(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.RefreshToken = cookie
|
req.RefreshToken = cookie
|
||||||
@@ -107,7 +107,7 @@ func RefreshToken(c *gin.Context) {
|
|||||||
|
|
||||||
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
|
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||||
if usn == 0 {
|
if usn == 0 {
|
||||||
http_resp.JsonUnauthorized(c)
|
httpresp.JsonUnauthorized(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,11 +117,11 @@ func RefreshToken(c *gin.Context) {
|
|||||||
at, rt, err := sessionLogin(c, usn)
|
at, rt, err := sessionLogin(c, usn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn)
|
log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn)
|
||||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http_resp.JsonOK(c, http_resp.Success(&RefreshTokenResp{
|
httpresp.JsonOK(c, httpresp.Success(&RefreshTokenResp{
|
||||||
AccessToken: at,
|
AccessToken: at,
|
||||||
RefreshToken: rt,
|
RefreshToken: rt,
|
||||||
}))
|
}))
|
||||||
@@ -137,13 +137,13 @@ type LogoutResp struct {
|
|||||||
func Logout(c *gin.Context) {
|
func Logout(c *gin.Context) {
|
||||||
req := &LogoutReq{}
|
req := &LogoutReq{}
|
||||||
if err := c.ShouldBindJSON(req); err != nil {
|
if err := c.ShouldBindJSON(req); err != nil {
|
||||||
http_resp.JsonBadRequest(c)
|
httpresp.JsonBadRequest(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.RefreshToken == "" {
|
if req.RefreshToken == "" {
|
||||||
cookie, err := c.Cookie("refresh_token")
|
cookie, err := c.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http_resp.JsonUnauthorized(c)
|
httpresp.JsonUnauthorized(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.RefreshToken = cookie
|
req.RefreshToken = cookie
|
||||||
@@ -151,5 +151,5 @@ func Logout(c *gin.Context) {
|
|||||||
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||||
log.Errorf("Logout sessionLogout error: %v", err)
|
log.Errorf("Logout sessionLogout error: %v", err)
|
||||||
}
|
}
|
||||||
http_resp.JsonOK(c, http_resp.Success(&LogoutResp{}))
|
httpresp.JsonOK(c, httpresp.Success(&LogoutResp{}))
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package http_handler
|
package httphandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bou.ke/monkey"
|
"bou.ke/monkey"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb/mocks"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -30,21 +30,21 @@ func (ts *LoginTestSuite) TestLogin() {
|
|||||||
Code: "",
|
Code: "",
|
||||||
})
|
})
|
||||||
Login(c)
|
Login(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, http_resp.ParamError)
|
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||||
})
|
})
|
||||||
|
|
||||||
ts.Run("UserNewClientLB Failed", func() {
|
ts.Run("UserNewClientLB Failed", func() {
|
||||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||||
return nil, assert.AnError
|
return nil, assert.AnError
|
||||||
})
|
})
|
||||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||||
|
|
||||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||||
Phone: "13800000000",
|
Phone: "13800000000",
|
||||||
Code: "1234",
|
Code: "1234",
|
||||||
})
|
})
|
||||||
Login(c)
|
Login(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.Failed)
|
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||||
})
|
})
|
||||||
|
|
||||||
ts.Run("Login Failed", func() {
|
ts.Run("Login Failed", func() {
|
||||||
@@ -55,17 +55,17 @@ func (ts *LoginTestSuite) TestLogin() {
|
|||||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||||
Return(nil, fmt.Errorf("gRPC failed"))
|
Return(nil, fmt.Errorf("gRPC failed"))
|
||||||
|
|
||||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||||
return client, nil
|
return client, nil
|
||||||
})
|
})
|
||||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||||
|
|
||||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||||
Phone: "13800000000",
|
Phone: "13800000000",
|
||||||
Code: "1234",
|
Code: "1234",
|
||||||
})
|
})
|
||||||
Login(c)
|
Login(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.Failed)
|
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||||
})
|
})
|
||||||
|
|
||||||
ts.Run("OK", func() {
|
ts.Run("OK", func() {
|
||||||
@@ -74,19 +74,19 @@ func (ts *LoginTestSuite) TestLogin() {
|
|||||||
client := mocks.NewMockUserClient(ctrl)
|
client := mocks.NewMockUserClient(ctrl)
|
||||||
client.EXPECT().
|
client.EXPECT().
|
||||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||||
Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
Return(&rspb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||||
|
|
||||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||||
return client, nil
|
return client, nil
|
||||||
})
|
})
|
||||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||||
|
|
||||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||||
Phone: "13800000000",
|
Phone: "13800000000",
|
||||||
Code: "1234",
|
Code: "1234",
|
||||||
})
|
})
|
||||||
Login(c)
|
Login(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.OK)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
|||||||
RefreshToken: "",
|
RefreshToken: "",
|
||||||
})
|
})
|
||||||
RefreshToken(c)
|
RefreshToken(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, http_resp.ParamError)
|
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||||
})
|
})
|
||||||
|
|
||||||
ts.Run("ParseToken Failed", func() {
|
ts.Run("ParseToken Failed", func() {
|
||||||
@@ -111,7 +111,7 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
|||||||
RefreshToken: "abc",
|
RefreshToken: "abc",
|
||||||
})
|
})
|
||||||
RefreshToken(c)
|
RefreshToken(c)
|
||||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.TokenInvalid)
|
||||||
})
|
})
|
||||||
|
|
||||||
//ts.Run("Redis Get Failed", func() {
|
//ts.Run("Redis Get Failed", func() {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package http_handler
|
package httphandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/sspb"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,23 +15,23 @@ import (
|
|||||||
func (c *Client) handle(event Event) {
|
func (c *Client) handle(event Event) {
|
||||||
switch e := event.(type) {
|
switch e := event.(type) {
|
||||||
case *ClientEvent:
|
case *ClientEvent:
|
||||||
msg := &ss_pb.Message{}
|
msg := &sspb.Message{}
|
||||||
if err := proto.Unmarshal(e.Msg, msg); err != nil {
|
if err := proto.Unmarshal(e.Msg, msg); err != nil {
|
||||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||||
c.cancel()
|
c.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch msg.ID {
|
switch msg.ID {
|
||||||
case ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
case sspb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
||||||
m := &ss_pb.C2S_EnterInstance{}
|
m := &sspb.C2S_EnterInstance{}
|
||||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||||
c.cancel()
|
c.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.onEnter(m)
|
c.onEnter(m)
|
||||||
case ss_pb.MessageID_MESSAGE_ID_ACTION:
|
case sspb.MessageID_MESSAGE_ID_ACTION:
|
||||||
m := &ss_pb.C2S_Action{}
|
m := &sspb.C2S_Action{}
|
||||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||||
c.cancel()
|
c.cancel()
|
||||||
@@ -46,20 +46,20 @@ func (c *Client) handle(event Event) {
|
|||||||
c.Status = 1
|
c.Status = 1
|
||||||
UserMgr.Add(c.USN, c)
|
UserMgr.Add(c.USN, c)
|
||||||
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
|
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
|
||||||
c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{
|
c.WriteMessage(sspb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &sspb.S2C_LoginSuccess{
|
||||||
InstanceID: 1,
|
InstanceID: 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
func (c *Client) onEnter(msg *sspb.C2S_EnterInstance) {
|
||||||
client, err := grpc_client.SceneNewClientLB()
|
client, err := grpcclient.SceneNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("SceneNewClient err: %v", err)
|
c.logger.Errorf("SceneNewClient err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
|
resp, err := client.Enter(c.ctx, &rspb.EnterReq{
|
||||||
USN: c.USN,
|
USN: c.USN,
|
||||||
GatewaySID: global.GatewaySID,
|
GatewaySID: global.GatewaySID,
|
||||||
InstanceID: msg.InstanceID,
|
InstanceID: msg.InstanceID,
|
||||||
@@ -71,19 +71,19 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
|||||||
c.SceneSID = resp.SceneSID
|
c.SceneSID = resp.SceneSID
|
||||||
c.UniqueNo = resp.UniqueNo
|
c.UniqueNo = resp.UniqueNo
|
||||||
c.InstanceID = msg.InstanceID
|
c.InstanceID = msg.InstanceID
|
||||||
c.WriteBytes(ss_pb.MessageID(resp.MessageID), resp.Payload)
|
c.WriteBytes(sspb.MessageID(resp.MessageID), resp.Payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) onLeave() {
|
func (c *Client) onLeave() {
|
||||||
if c.SceneSID == 0 {
|
if c.SceneSID == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
client, err := grpcclient.SceneNewClient(c.SceneSID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("SceneNewClient err: %v", err)
|
c.logger.Errorf("SceneNewClient err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = client.Leave(context.Background(), &grpc_pb.LeaveReq{
|
_, err = client.Leave(context.Background(), &rspb.LeaveReq{
|
||||||
USN: c.USN,
|
USN: c.USN,
|
||||||
UniqueNo: c.UniqueNo,
|
UniqueNo: c.UniqueNo,
|
||||||
})
|
})
|
||||||
@@ -93,11 +93,11 @@ func (c *Client) onLeave() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
func (c *Client) onAction(msg *sspb.C2S_Action) {
|
||||||
if c.SceneSID == 0 {
|
if c.SceneSID == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
if err := grpcclient.SendMessageToScene(c.SceneSID, grpcclient.FunAction, &rspb.ActionReq{
|
||||||
UniqueNo: c.UniqueNo,
|
UniqueNo: c.UniqueNo,
|
||||||
USN: c.USN,
|
USN: c.USN,
|
||||||
Action: int32(msg.Action),
|
Action: int32(msg.Action),
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/sspb"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteMessage 向客户端发送消息
|
// WriteMessage 向客户端发送消息
|
||||||
func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
func (c *Client) WriteMessage(id sspb.MessageID, data proto.Message) {
|
||||||
if c.conn == nil || c.conn.IsClose() {
|
if c.conn == nil || c.conn.IsClose() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
|||||||
c.logger.Errorf("WriteMessage proto.Marshal err: %v", err)
|
c.logger.Errorf("WriteMessage proto.Marshal err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m, err := proto.Marshal(&ss_pb.Message{
|
m, err := proto.Marshal(&sspb.Message{
|
||||||
ID: id,
|
ID: id,
|
||||||
Payload: d,
|
Payload: d,
|
||||||
})
|
})
|
||||||
@@ -29,11 +29,11 @@ func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteBytes 向客户端发送字节数据
|
// WriteBytes 向客户端发送字节数据
|
||||||
func (c *Client) WriteBytes(id ss_pb.MessageID, data []byte) {
|
func (c *Client) WriteBytes(id sspb.MessageID, data []byte) {
|
||||||
if c.conn == nil || c.conn.IsClose() {
|
if c.conn == nil || c.conn.IsClose() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m, err := proto.Marshal(&ss_pb.Message{
|
m, err := proto.Marshal(&sspb.Message{
|
||||||
ID: id,
|
ID: id,
|
||||||
Payload: data,
|
Payload: data,
|
||||||
})
|
})
|
||||||
@@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/sspb"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/wshandler/client"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -109,8 +109,8 @@ func (l *Login) Stop() {
|
|||||||
// StartLogin 开始登录流程
|
// StartLogin 开始登录流程
|
||||||
func (l *Login) StartLogin(user *User) {
|
func (l *Login) StartLogin(user *User) {
|
||||||
if !l.CheckToken(user) {
|
if !l.CheckToken(user) {
|
||||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||||
ID: ss_pb.KickOutID_KICK_OUT_ID_TOKEN_INVALID,
|
ID: sspb.KickOutID_KICK_OUT_ID_TOKEN_INVALID,
|
||||||
})
|
})
|
||||||
user.Cli.CloseClient()
|
user.Cli.CloseClient()
|
||||||
return
|
return
|
||||||
@@ -118,8 +118,8 @@ func (l *Login) StartLogin(user *User) {
|
|||||||
if gatewaySID := l.CheckOnline(user); len(gatewaySID) > 0 {
|
if gatewaySID := l.CheckOnline(user); len(gatewaySID) > 0 {
|
||||||
// 如果在线就要踢,如果踢失败了就返回服务器繁忙,一般不应该走到这里
|
// 如果在线就要踢,如果踢失败了就返回服务器繁忙,一般不应该走到这里
|
||||||
if !l.KickUser(user.Cli.SceneSID, user.Cli.USN) {
|
if !l.KickUser(user.Cli.SceneSID, user.Cli.USN) {
|
||||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||||
ID: ss_pb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
ID: sspb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||||
})
|
})
|
||||||
user.Cli.CloseClient()
|
user.Cli.CloseClient()
|
||||||
return
|
return
|
||||||
@@ -128,8 +128,8 @@ func (l *Login) StartLogin(user *User) {
|
|||||||
if client.UserMgr.GetSize() >= global.MaxOnlineSize {
|
if client.UserMgr.GetSize() >= global.MaxOnlineSize {
|
||||||
// 如果人数满了就排队
|
// 如果人数满了就排队
|
||||||
if err := GetQueueUp().Enqueue(user.Cli); err != nil {
|
if err := GetQueueUp().Enqueue(user.Cli); err != nil {
|
||||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||||
ID: ss_pb.KickOutID_KICK_OUT_ID_QUEUE_UP_FULL,
|
ID: sspb.KickOutID_KICK_OUT_ID_QUEUE_UP_FULL,
|
||||||
})
|
})
|
||||||
user.Cli.CloseClient()
|
user.Cli.CloseClient()
|
||||||
return
|
return
|
||||||
@@ -137,13 +137,13 @@ func (l *Login) StartLogin(user *User) {
|
|||||||
// 告诉客户端正在排队
|
// 告诉客户端正在排队
|
||||||
position, ok := GetQueueUp().GetPosition(user.Cli.USN)
|
position, ok := GetQueueUp().GetPosition(user.Cli.USN)
|
||||||
if !ok {
|
if !ok {
|
||||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||||
ID: ss_pb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
ID: sspb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||||
})
|
})
|
||||||
user.Cli.CloseClient()
|
user.Cli.CloseClient()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_QUEUE_UP, &ss_pb.S2C_QueueUp{
|
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_QUEUE_UP, &sspb.S2C_QueueUp{
|
||||||
QueueUpCount: int32(position),
|
QueueUpCount: int32(position),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -165,12 +165,12 @@ func (l *Login) CheckOnline(user *User) string {
|
|||||||
|
|
||||||
// KickUser 把玩家踢下线
|
// KickUser 把玩家踢下线
|
||||||
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
||||||
gc, err := grpc_client.GatewayNewClient(gatewaySID)
|
gc, err := grpcclient.GatewayNewClient(gatewaySID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_, err = gc.KickUser(l.ctx, &grpc_pb.KickUserReq{USN: usn})
|
_, err = gc.KickUser(l.ctx, &rspb.KickUserReq{USN: usn})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("KickUser err: %v, sid: %v, usn: %v", err, gatewaySID, usn)
|
log.Errorf("KickUser err: %v, sid: %v, usn: %v", err, gatewaySID, usn)
|
||||||
return false
|
return false
|
||||||
@@ -3,7 +3,7 @@ package login
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/wshandler/client"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package http_gateway
|
package httpgateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
@@ -47,7 +47,7 @@ func authJwt() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
pList := strings.SplitN(c.Request.URL.Path, "/", 4)
|
pList := strings.SplitN(c.Request.URL.Path, "/", 4)
|
||||||
if len(pList) < 4 || pList[2] == "" {
|
if len(pList) < 4 || pList[2] == "" {
|
||||||
http_resp.JsonNotFound(c)
|
httpresp.JsonNotFound(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -60,14 +60,14 @@ func authJwt() gin.HandlerFunc {
|
|||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http_resp.JsonUnauthorized(c)
|
httpresp.JsonUnauthorized(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayAccessToken+token, (&utils.UserSession{}).GetUsnKey()).Int64()
|
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayAccessToken+token, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||||
if usn == 0 {
|
if usn == 0 {
|
||||||
http_resp.JsonUnauthorized(c)
|
httpresp.JsonUnauthorized(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
package http_gateway
|
package httpgateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/http_handler"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/httphandler"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/http_gateway/wrapper"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/net/httpgateway/wrapper"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
@@ -61,11 +61,11 @@ func InitRouter() *gin.Engine {
|
|||||||
|
|
||||||
r.HandleMethodNotAllowed = true
|
r.HandleMethodNotAllowed = true
|
||||||
r.NoMethod(func(c *gin.Context) {
|
r.NoMethod(func(c *gin.Context) {
|
||||||
http_resp.JsonMethodNotAllowed(c)
|
httpresp.JsonMethodNotAllowed(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
})
|
})
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
http_resp.JsonNotFound(c)
|
httpresp.JsonNotFound(c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -85,22 +85,22 @@ func InitRouter() *gin.Engine {
|
|||||||
func initGatewayPath(r *gin.RouterGroup) {
|
func initGatewayPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/" + common.KeyDiscoverServiceNameGateway)
|
g := r.Group("/" + common.KeyDiscoverServiceNameGateway)
|
||||||
|
|
||||||
g.POST("/open/login", http_handler.Login)
|
g.POST("/open/login", httphandler.Login)
|
||||||
g.POST("/open/refresh_token", http_handler.RefreshToken)
|
g.POST("/open/refresh_token", httphandler.RefreshToken)
|
||||||
g.POST("/open/logout", http_handler.Logout)
|
g.POST("/open/logout", httphandler.Logout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initUserPath(r *gin.RouterGroup) {
|
func initUserPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/" + common.KeyDiscoverServiceNameUser)
|
g := r.Group("/" + common.KeyDiscoverServiceNameUser)
|
||||||
|
|
||||||
client, err := grpc_client.UserNewClientLB()
|
client, err := grpcclient.UserNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get user conn failed: %v", err)
|
log.Errorf("get user conn failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gwMux := InitServeMux()
|
gwMux := InitServeMux()
|
||||||
if err = grpc_pb.RegisterUserHandlerClient(context.Background(), gwMux, client); err != nil {
|
if err = rspb.RegisterUserHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||||
log.Errorf("RegisterUserHandlerClient err: %v", err)
|
log.Errorf("RegisterUserHandlerClient err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -111,14 +111,14 @@ func initUserPath(r *gin.RouterGroup) {
|
|||||||
func initQgdzsPath(r *gin.RouterGroup) {
|
func initQgdzsPath(r *gin.RouterGroup) {
|
||||||
g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs)
|
g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs)
|
||||||
|
|
||||||
client, err := grpc_client.QgdzsNewClientLB()
|
client, err := grpcclient.QgdzsNewClientLB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get qgdzs conn failed: %v", err)
|
log.Errorf("get qgdzs conn failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gwMux := InitServeMux()
|
gwMux := InitServeMux()
|
||||||
if err = grpc_pb.RegisterQgdzsHandlerClient(context.Background(), gwMux, client); err != nil {
|
if err = rspb.RegisterQgdzsHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||||
log.Errorf("RegisterQgdzsHandlerClient err: %v", err)
|
log.Errorf("RegisterQgdzsHandlerClient err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,8 @@ package wrapper
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rs_common"
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/rscommon"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
@@ -17,32 +17,32 @@ func ErrorHandler(_ context.Context, _ *runtime.ServeMux, _ runtime.Marshaler, w
|
|||||||
if !ok {
|
if !ok {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_ = json.NewEncoder(w).Encode(http_resp.Error(http_resp.Failed))
|
_ = json.NewEncoder(w).Encode(httpresp.Error(httpresp.Failed))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
code, msg := 0, ""
|
code, msg := 0, ""
|
||||||
for _, detail := range st.Details() {
|
for _, detail := range st.Details() {
|
||||||
if errorInfo, ok := detail.(*rs_common.ErrorInfo); ok {
|
if errorInfo, ok := detail.(*rscommon.ErrorInfo); ok {
|
||||||
code = int(errorInfo.Code)
|
code = int(errorInfo.Code)
|
||||||
msg = errorInfo.Msg
|
msg = errorInfo.Msg
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if code == 0 {
|
if code == 0 {
|
||||||
code = http_resp.Failed.Code()
|
code = httpresp.Failed.Code()
|
||||||
msg = http_resp.Failed.Error()
|
msg = httpresp.Failed.Error()
|
||||||
}
|
}
|
||||||
if st.Code() == codes.Unknown || st.Code() == codes.Unimplemented || st.Code() == codes.NotFound {
|
if st.Code() == codes.Unknown || st.Code() == codes.Unimplemented || st.Code() == codes.NotFound {
|
||||||
msg = st.Message()
|
msg = st.Message()
|
||||||
}
|
}
|
||||||
if st.Code() == codes.NotFound {
|
if st.Code() == codes.NotFound {
|
||||||
code = http_resp.NotFound.Code()
|
code = httpresp.NotFound.Code()
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(grpcCodeToHTTPCode(st.Code()))
|
w.WriteHeader(grpcCodeToHTTPCode(st.Code()))
|
||||||
_ = json.NewEncoder(w).Encode(http_resp.Error(http_resp.NewCode(code, msg)))
|
_ = json.NewEncoder(w).Encode(httpresp.Error(httpresp.NewCode(code, msg)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这里定义 Internal 属于业务错误,其他的属于 500 报错
|
// 这里定义 Internal 属于业务错误,其他的属于 500 报错
|
||||||
@@ -2,7 +2,7 @@ package wrapper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@@ -20,9 +20,9 @@ func NewWrappedMarshaler(inner runtime.Marshaler) *WrappedMarshaler {
|
|||||||
func (w *WrappedMarshaler) Marshal(v interface{}) ([]byte, error) {
|
func (w *WrappedMarshaler) Marshal(v interface{}) ([]byte, error) {
|
||||||
dataBytes, err := w.inner.Marshal(v)
|
dataBytes, err := w.inner.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return json.Marshal(http_resp.Error(http_resp.Failed))
|
return json.Marshal(httpresp.Error(httpresp.Failed))
|
||||||
}
|
}
|
||||||
return json.Marshal(http_resp.Success(json.RawMessage(dataBytes)))
|
return json.Marshal(httpresp.Success(json.RawMessage(dataBytes)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WrappedMarshaler) Unmarshal(data []byte, v interface{}) error {
|
func (w *WrappedMarshaler) Unmarshal(data []byte, v interface{}) error {
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package ws_gateway
|
package wsgateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/wshandler/client"
|
||||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
|
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/wshandler/login"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user