This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Server/gateway/handler/ws_handler/handler.go

85 lines
2.0 KiB
Go

package ws_handler
import (
"common/net/grpc/service"
"common/proto/sc/sc_pb"
"common/proto/ss/grpc_pb"
"gateway/grpc_server/stream_client"
"google.golang.org/protobuf/proto"
"time"
)
func (c *Client) handle(event Event) {
switch e := event.(type) {
case *ClientEvent:
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()
return
}
c.logger.Infof("收到客户端消息:%v", msg.ID)
switch msg.ID {
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 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()
return
}
c.onAction(m)
}
case *PongEvent:
c.heartBeat = time.Now()
}
}
func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) {
client, err := service.SceneNewClient()
if err != nil {
c.logger.Errorf("SceneNewClient err: %v", err)
return
}
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
UID: int32(c.UID),
GatewaySID: GatewaySID,
InstanceID: msg.InstanceID,
})
if err != nil {
c.logger.Errorf("enter err: %v", err)
return
}
c.SceneSID = resp.SceneSID
c.UniqueNo = resp.UniqueNo
c.InstanceID = int(msg.InstanceID)
c.WriteMessage(sc_pb.MessageID_MESSAGE_ID_POSITION, &sc_pb.S2C_Position{
Info: []*sc_pb.PositionInfo{{
UID: int32(c.UID),
X: 1,
Y: 1,
}},
})
}
func (c *Client) onAction(msg *sc_pb.C2S_Action) {
if c.SceneSID == 0 {
return
}
if err := stream_client.SendMessageToScene(c.SceneSID, stream_client.FunAction, &grpc_pb.ActionReq{
UniqueNo: c.UniqueNo,
UID: int32(c.UID),
Action: int32(msg.Action),
Payload: msg.Payload,
}); err != nil {
c.logger.Errorf("send action err: %v", err)
}
}