Photon——Adding Operations 添加操作

 

Adding Operations 添加操作

     In many cases, just sending events is not enough for a game. If you want to provide authorization, persistency or game-specific Operations, it’s time to extend Lite (or LiteLobby).This page shows two ways to implement new Operations and how to use them. Our sample Operation expects a message from the client, checks it and provides return values.
     在许多情况下,只是发送事件是不够的,对于一个游戏。如果你想要提供授权、持续性或游戏操作,它是需要扩展Lite(或LiteLobby)。这个页面显示了两个方法来实现新业务和如何使用他们。我们的示例操作预计消息来自客户端,检查它并提供返回值。

Server-Side, Simple Variant 服务端,简单的变体

     Let’s start on the server side, in the Lite Application. This variant is not very elegant but simple.
     让我们在服务端开始,在Lite应用中,这变体不是很优雅但是很简单
 
 
//Server SDK, LitePeer.cs
public void OnOperationRequest(OperationRequest request)
{
     // handle operation here (check request.OperationCode)
     switch (request.OperationCode)
     {
          case 1:
        {
           var message = ( string )request.Params[100];
           if (message == "Hello World" )
           {
              // received hello world, send an answer!
              var response = new OperationResponse(request, 0, "OK" , new Dictionary< short , object > { { 100, "Hello yourself!" } } );
              this .PhotonPeer.SendOperationResponse(response);
           }
           else
           {
              // received something else, send an error
              var response = new OperationResponse(request, 1, "Don't understand, what are you saying?" );
              this .PhotonPeer.SendOperationResponse(response);
           }
 
           break ;
        }
     }
}
 
     The above code first checks the called OperationCode. In this case, it’s 1. In Photon, the OperationCode is a shortcut for the name/type of an Operation. We’ll see how the client call this below.
     上面的代码首先检查OperationCode的调用。在这种情况下,它是1。在Photon,OperationCode是一个操作的名称或类型的快捷方式。下面我们将看到客户端是如何调用这个。
 
     If Operation 1 is called, we check the request parameters. Here, parameter 100 is expected to be a string. Again, Photon only uses byte-typed parameters, to keep things lean during transfer.
     如果操作1被调用,我们检查请求参数。在这里, 参数100预计是一个字符串。再一次,Photon只使用字节输入参数,为了保持精简传递。
 
     The code then checks the message content and prepares a response. Both responses have a returnCode and a debug message. The returnCode 0 is a positive return, any other number (here: 1) could mean an error and needs to be handled by the client.
     然后该代码检查消息内容和准备一个响应。两端的响应有一个returnCode和调试消息。这个returnCode 0是一个积极的返回值,任何其他号码(这里是:1)可能意味着一个错误和需要由客户端处理。
 
     Our positive response also includes a return value. You could add more key-value pairs, but here we stick to 100, which is a string.
     我们积极响应还包括一个返回值。你可以添加更多的键-值对,但是这里我们坚持到100,这是一个字符串。
 
     This already is a complete implementation of an Operation. This is our convention for a new Operation, which must be carried over to the client to be used.
     这已经是一个完整的操作实现。这是我们约定的新操作,必须被传递到客户端使用。

Client Side 客户端

     Knowing the definition of above’s Operation, we can call it from the client side. This client code calls it on connect:
     知道了上面操作的定义,我们可以从客户端调用。客户端代码调用它在连接时候:
 
public void PeerStatusCallback( int returnCode)
{
     // handle peer status callback
     switch ((ReturnCode)returnCode)
     {
     case ReturnCode.Connect:
         // send hello world when connected
         var parameter = new Hashtable { { ( byte )100, "Hello World" } };
         this .Peer.OpCustom(1, parameter, true );
         break ;
         //[...]
 
 
     As defined above, we always expect a message as parameter 100. This is provided as parameter Hashtable. We make sure that the parameter key is not mistaken for anything but a byte, or else it won’t send. And we put in: “Hello World”.
     如上所述,我们总是期待一个消息作为参数100。 这是提供的参数散列表。我们确保参数key没有误认为是任何别的东西,否则它不会发送。我们输出:“Hello World”。
 
     The PhotonPeer (and LitePeer) method OpCustom expects the OperationCode as first parameter. This is 1. The parameters follow and we want to make sure the operation arrives (it’s essential after all).
     这个PhotonPeer(和LitePeer)的方法OpCustom预计OperationCode作为第一个参数。这里是1。这个参数遵循并是我们想要的来确保操作到达。
 
     Aside from this, the client just has to do the usual work, which means it has to call PhotonPeer.Service in intervals.
     除了这个,客户端只需要做平时的工作,这意味着它必须调用PhotonPeer.Service在时间间隔的时候。
 
     Once the result is available, it can be handled like this:
     一旦结果是可用的,它可以处理像这样:
 
 
public void OperationResponse(OperationResponse operationResponse)
{
     // handle response by code (action we called)
     switch (operationResponse.OperationCode)
     {
         // out custom "hello world" operation's code is 1
         case 1:
             // OK
             if (operationResponse.ReturnCode == 0)
             {
                 // show the complete content of the response
                 Console.WriteLine(operationResponse.ToStringFull());
             }
             else
             {
                 // show the error message
                 Console.WriteLine(operationResponse.DebugMessage);
             }
             break ;
     }
}

Server Side, Advanced Version 服务端,高级版本

     The preferred way to handle Operations would be to create a class for it. This way it becomes strongly typed and issues due to missing parameters are handled by the framework.
     首选的办法是为处理操作创建一个类。这种方式是强类型的和失踪的参数由framework来处理。
 
     This is the definition of the Operation, its parameters, their types and of the return values:
     这是操作的定义,它的参数,它的类型和返回值:
 
 
//new Operation Class
namespace MyPhotonServer
{
     using Photon.SocketServer;
     using Photon.SocketServer.Rpc;
 
     public class MyCustomOperation : Operation
     {
         public MyCustomOperation(OperationRequest request) : base (request)
         {
         }
 
         [RequestParameter(Code = 100, IsOptional = false )]
         [ResponseParameter(Code = 100, IsOptional = false )]
         public string Message { get ; set ; }
     }
}
 
     With the Operation class defined above, we can map the request to it and also provide the response in a strongly typed manner.
     上面是操作类的定义,我们可以请求映射到它和在一个强类型的方式中提供响应
 
public void OnOperationRequest(OperationRequest request)
{
     switch (request.OperationCode)
     {
         case 1:
         {
             var operation = new MyCustomOperation(request);
             if (peer.ValidateOperation(operation) == false )
             {
                 // received garbage, send an error
                 var response = new OperationResponse(request, 1, "That's garbage!" );
                 this .PhotonPeer.SendOperationResponse(response);
                 return ;
             }
 
             if (operation.Message == "Hello World" )
             {
                 // received hello world, send an answer!
                 operation.Message = "Hello yourself!" ;
                 OperationResponse response = operation.GetOperationResponse(0, "OK" );
                 this .PhotonPeer.SendOperationResponse(response);
             }
             else
             {
                 // received something else, send an error
                 var response = new OperationResponse(request, 1, "Don't understand, what are you saying?" );
                 this .PhotonPeer.SendOperationResponse(response);
             }
             break ;
         }
     }
}

你可能感兴趣的:(Opera)