feat 协议移动到Public
This commit is contained in:
@@ -20,7 +20,7 @@ type ModuleWebServer struct {
|
||||
|
||||
func (m *ModuleWebServer) Init() error {
|
||||
m.wg = &sync.WaitGroup{}
|
||||
m.router = http_gateway.InitRouter(config.Get())
|
||||
m.router = http_gateway.InitRouter()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,8 @@ package server
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"common/proto/gen/common"
|
||||
"common/proto/gen/cs"
|
||||
"common/proto/gen/grpc_pb"
|
||||
"common/proto/sc/sc_pb"
|
||||
"common/proto/ss/grpc_pb"
|
||||
"gateway/handler/ws_handler"
|
||||
"sync"
|
||||
)
|
||||
@@ -25,16 +24,16 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
} else {
|
||||
if args.UID == -1 {
|
||||
for _, client := range ws_handler.UserMgr.GetAll() {
|
||||
client.WriteBytes(cs.MessageID(args.MessageID), args.Payload)
|
||||
client.WriteBytes(sc_pb.MessageID(args.MessageID), args.Payload)
|
||||
}
|
||||
} else {
|
||||
if client := ws_handler.UserMgr.GetByUID(int(args.UID)); client != nil {
|
||||
client.WriteBytes(cs.MessageID(args.MessageID), args.Payload)
|
||||
client.WriteBytes(sc_pb.MessageID(args.MessageID), args.Payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
return server.SendAndClose(&common.Empty{})
|
||||
return server.SendAndClose(&grpc_pb.ToClientResp{})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package server
|
||||
import (
|
||||
"common/discover/common"
|
||||
"common/net/grpc/service"
|
||||
"common/proto/gen/grpc_pb"
|
||||
"common/proto/ss/grpc_pb"
|
||||
"gateway/handler/ws_handler"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
@@ -20,6 +20,15 @@ func Json(c *gin.Context, code *Code, data interface{}) {
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func JsonByStatus(c *gin.Context, status int, code *Code, data interface{}) {
|
||||
result := &RespJsonData{
|
||||
Code: code.Code(),
|
||||
Msg: code.Message(),
|
||||
Data: data,
|
||||
}
|
||||
c.JSON(status, result)
|
||||
}
|
||||
|
||||
func AbortJson(c *gin.Context, code *Code, data interface{}) {
|
||||
result := &RespJsonData{
|
||||
Code: code.Code(),
|
||||
|
||||
@@ -3,7 +3,7 @@ package ws_handler
|
||||
import (
|
||||
"common/log"
|
||||
"common/net/socket"
|
||||
"common/proto/gen/cs"
|
||||
"common/proto/sc/sc_pb"
|
||||
"context"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
@@ -85,13 +85,13 @@ func (c *Client) OnEvent(event Event) {
|
||||
}
|
||||
|
||||
// WriteMessage 向客户端发送消息
|
||||
func (c *Client) WriteMessage(id cs.MessageID, data proto.Message) {
|
||||
func (c *Client) WriteMessage(id sc_pb.MessageID, data proto.Message) {
|
||||
d, err := proto.Marshal(data)
|
||||
if err != nil {
|
||||
c.logger.Errorf("WriteMessage proto.Marshal err: %v", err)
|
||||
return
|
||||
}
|
||||
m, err := proto.Marshal(&cs.Message{
|
||||
m, err := proto.Marshal(&sc_pb.Message{
|
||||
ID: id,
|
||||
Payload: d,
|
||||
})
|
||||
@@ -105,8 +105,8 @@ func (c *Client) WriteMessage(id cs.MessageID, data proto.Message) {
|
||||
}
|
||||
|
||||
// WriteBytes 向客户端发送字节数据
|
||||
func (c *Client) WriteBytes(id cs.MessageID, data []byte) {
|
||||
m, err := proto.Marshal(&cs.Message{
|
||||
func (c *Client) WriteBytes(id sc_pb.MessageID, data []byte) {
|
||||
m, err := proto.Marshal(&sc_pb.Message{
|
||||
ID: id,
|
||||
Payload: data,
|
||||
})
|
||||
|
||||
@@ -2,8 +2,8 @@ package ws_handler
|
||||
|
||||
import (
|
||||
"common/net/grpc/service"
|
||||
"common/proto/gen/cs"
|
||||
"common/proto/gen/grpc_pb"
|
||||
"common/proto/sc/sc_pb"
|
||||
"common/proto/ss/grpc_pb"
|
||||
"gateway/grpc_server/stream_client"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"time"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
func (c *Client) handle(event Event) {
|
||||
switch e := event.(type) {
|
||||
case *ClientEvent:
|
||||
msg := &cs.Message{}
|
||||
msg := &sc_pb.Message{}
|
||||
if err := proto.Unmarshal(e.Msg, msg); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
@@ -20,16 +20,16 @@ func (c *Client) handle(event Event) {
|
||||
}
|
||||
c.logger.Infof("收到客户端消息:%v", msg.ID)
|
||||
switch msg.ID {
|
||||
case cs.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
||||
m := &cs.C2S_EnterInstance{}
|
||||
case sc_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
||||
m := &sc_pb.C2S_EnterInstance{}
|
||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
return
|
||||
}
|
||||
c.onEnter(m)
|
||||
case cs.MessageID_MESSAGE_ID_ACTION:
|
||||
m := &cs.C2S_Action{}
|
||||
case sc_pb.MessageID_MESSAGE_ID_ACTION:
|
||||
m := &sc_pb.C2S_Action{}
|
||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
@@ -42,7 +42,7 @@ func (c *Client) handle(event Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) onEnter(msg *cs.C2S_EnterInstance) {
|
||||
func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) {
|
||||
client, err := service.SceneNewClient()
|
||||
if err != nil {
|
||||
c.logger.Errorf("SceneNewClient err: %v", err)
|
||||
@@ -60,8 +60,8 @@ func (c *Client) onEnter(msg *cs.C2S_EnterInstance) {
|
||||
c.SceneSID = resp.SceneSID
|
||||
c.UniqueNo = resp.UniqueNo
|
||||
c.InstanceID = int(msg.InstanceID)
|
||||
c.WriteMessage(cs.MessageID_MESSAGE_ID_POSITION, &cs.S2C_Position{
|
||||
Info: []*cs.PositionInfo{{
|
||||
c.WriteMessage(sc_pb.MessageID_MESSAGE_ID_POSITION, &sc_pb.S2C_Position{
|
||||
Info: []*sc_pb.PositionInfo{{
|
||||
UID: int32(c.UID),
|
||||
X: 1,
|
||||
Y: 1,
|
||||
@@ -69,7 +69,7 @@ func (c *Client) onEnter(msg *cs.C2S_EnterInstance) {
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) onAction(msg *cs.C2S_Action) {
|
||||
func (c *Client) onAction(msg *sc_pb.C2S_Action) {
|
||||
if c.SceneSID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package http_gateway
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"gateway/config"
|
||||
"gateway/handler/http_handler"
|
||||
"gateway/handler/http_handler/helper/render"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func InitRouter(cfg *config.Config) *gin.Engine {
|
||||
func InitRouter() *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
r := gin.New()
|
||||
@@ -21,16 +21,10 @@ func InitRouter(cfg *config.Config) *gin.Engine {
|
||||
|
||||
r.HandleMethodNotAllowed = true
|
||||
r.NoMethod(func(c *gin.Context) {
|
||||
c.JSON(http.StatusMethodNotAllowed, gin.H{
|
||||
"result": false,
|
||||
"error": "Method Not Allowed",
|
||||
})
|
||||
render.JsonByStatus(c, http.StatusMethodNotAllowed, render.Failed, "Method Not Allowed")
|
||||
})
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"result": false,
|
||||
"error": "Endpoint Not Found",
|
||||
})
|
||||
render.JsonByStatus(c, http.StatusNotFound, render.Failed, "Endpoint Not Found")
|
||||
})
|
||||
initBaseRouter(r)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user