package ws_handler import ( "common/net/grpc/service" "common/proto/gen/cs" "common/proto/gen/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 := &cs.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 cs.MessageID_MESSAGE_ID_ENTER_INSTANCE: m := &cs.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{} 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 *cs.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(cs.MessageID_MESSAGE_ID_POSITION, &cs.S2C_Position{ Info: []*cs.PositionInfo{{ UID: int32(c.UID), X: 1, Y: 1, }}, }) } func (c *Client) onAction(msg *cs.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) } }