Photon——Lite Concepts Lite概念

 

Lite Concepts Lite概念

     Here we will give you a quick overview of the basic concepts for the Lite application.
     这我们将给出一个Lite应用的基本概念
 

Peers 参与者

     Lite is based on the Application framework for Photon and also uses “Peer” as reference to a connected player. This is wrapped up and extended in the class LitePeer.When a client connects to the Lite Application, a new LitePeer is created in LiteApplication.CreatePeer. Further requests from that client (e.g. Operations) will now be handled by the corresponding LitePeer.In Lite, every LitePeer has a single RoomReference as State. Players can be only in a single room. The State is set by operation Join.The OnDisconnect method in LitePeer is called when a peer disconnects. If the peer is still in a room, this gets a message to remove the peer.
     Lite是基于Photon应用框架,也是使用Peer作为玩家的连接引用。这在LitePeer中被扩展的。当一个客户端连接到Lite应用,一个新的LitePeer被LiteApplication.CreatePeer创建。进一步的客户端请求将通过LitePeer被处理。在Lite,每一个LitePeer有一个独立的RoomReference状态。玩家们可以在一个独立的房间。当操作为加入时这状态将被设置。当peer断开时,LitePeer中的OnDisconnect方法会被调用,如果这个玩家人仍在一个房间内,将会获取一个移除peer的消息。

Rooms 房间

     In Lite, peers get into rooms to play together, which is why they are also called games. Rooms are responsible for everything. Everything aside from join, that’s it.Every game has a unique name and is treated as completely separated from any other. Also, every request that comes from a player is handled in sequence. Because many rooms exist in parallel and each request is handled in mere ticks, this setup scales well.Rooms have lists of players which they update on join, leave or disconnect. Between these players, events can be sent.Within a room, a player is also known as Actor and each Actor has an ActorNumber. That number is used to identify the origin of an event.
     在Lite中,参与者需要进入房间去和别人一起进行游戏。房间是要为所有东西负责的,除了加入。每一个游戏都有一个唯一的名字,并且是完全独立于其他游戏的。而且每个玩家的请求都会排队进行处理。因为许多的房间存在并行,且在短短的时间内处理请求,这样的设置能很好的进行扩展。房间有参与者的列表,当加入、离开或者断开时列表将被更新。在这些玩家中,事件可能会被发送。在一个房间内,每个玩家都是被当做Actor并且都有一个ActorNumber。这个编号是用于识别事件的源头。

Operations and Events 操作和事件

     Lite defines these Operations

  • Join: Enter any room by name. It gets created on the fly if it does not exist. Join will implicitly leave the previous room of a peer (if any). Returns the assigned ActorNumber.
  • Leave: Leaves the room (but keeps the connection).
  • RaiseEvent: Tells the room to send an event to the other peers in it. Events have an EventCode and can carry data provided by the client. Lite does not store events.
  • GetProperties: In Lite, properties can be attached to rooms and players. With this operation, they are fetched.
  • SetProperties: Attaches arbitrary key-value pairs ro a room or a single player. Lite does not use the data, so the client can send arbitrary data along. Code by convention.
     Lite定义了一下的操作
  • 加入: 输入任何房间的名字,如果它不存在的话它将被创建,加入时将隐式地离开先前的房间(如果有的话),返回指定的ActorNumber。
  • 离开: 离开房间(但保持连接)。
  • 触发事件: 告诉房间发送一个事件到其他Peer。事件有一个EventCode,可以携带客户端提供的数据。Lite并不存储事件。
  • 获取属性: 在Lite中,属性可以被附加到房间和玩家上,伴随着这个操作,他们都将被获取。
  • 设置属性: 高度的任意的键-值对,一个房间或一个单一的玩家,Lite不使用数据,所以客户端可以发送任意的数据,代码按照惯例。

     Lite defines these Events

  • Join: Joining a room (by Operation) updates the players list inside. This is sent in an event to all players (including the new one).
  • Leave: If a player leaves a room, the others are updated again.
  • Properties update: When properties are changed, there is the option to broadcast the change. This event takes care that every client is up to date.
     Lite定义了以下的事件
  • 连接: 加入一个房间(通过操作)更新房间内的玩家名单,这是发送一个事件给所有的玩家(包括新的那个)。
  • 离开: 如果一个玩家离开房间,其余的玩家将被再次更新。
  • 更新属性: 当属性被更改了,将广播这个更改。这个事件负责更新每个客户端为最新的。

RaiseEvent / Custom Events 触发事件与自定义事件

     With Lite, players can send events with arbitrary content to the other players in a room. The operation RaiseEvent takes care of this and forwards the event content accordingly.As an example, this client code from the DotNet Realtime Demo sends a player’s position:
     Lite中,玩家可以发送任意内容的事件到其他同一房间里的玩家。操作RaiseEvent负责这相应的事件内容。作为一个示例,该客户端代码与DotNet实时演示发送玩家的位置:
 
 
// Raises an event with the position data of this player.
internal void SendEvMove(LitePeer peer)
{
    if (peer == null)
    {
        return;
    }
 
    Hashtable evInfo = new Hashtable();
 
    evInfo.Add((byte)STATUS_PLAYER_POS_X, (byte)this.posX);
    evInfo.Add((byte)STATUS_PLAYER_POS_Y, (byte)this.posY);
    //OpRaiseEvent(byte eventCode, Hashtable evData, bool sendReliable, byte channelId, bool encrypt)
    peer.OpRaiseEvent(EV_MOVE, evInfo, isSendReliable, (byte)0, Game.RaiseEncrypted);
}
 
     The Lite Application does not try to interpret your events server-side, so you can send just about anything you want to. The EV_MOVE above is nothing Lite knows, it’s only defined and used client-side.The move event that’s raised in the sample above causes a EventAction call on the receiving clients. They handle the event and data like this:
     Lite应用程序并不会试图解释你的服务端事件,所以你可以发送任何你想要的。EV_MOVE是Lite不知道的,它仅仅在客户端被定义和使用。在一个EventAction调用客户端接收的情况下这个移动事件被触发。他们处理事件和数据如下:
 
//in Game.cs:
public void EventAction(byte eventCode, Hashtable photonEvent)
{
    int actorNr = 0;
    if (photonEvent.ContainsKey((byte)LiteEventKey.ActorNr))
    {
        actorNr = (int) photonEvent[(byte) LiteEventKey.ActorNr];
    }
 
    // get the player that raised this event
    Player p;
    this.Players.TryGetValue(actorNr, out p);
 
    switch (eventCode)
    {
        case Player.EV_MOVE:
            p.SetPosition((Hashtable)photonEvent[(byte)LiteEventKey.Data]);
            break;
        //[...]
    }
}
 
//in Player.cs:
internal void SetPosition(Hashtable evData)
{
    this.posX = (byte)evData[(byte)STATUS_PLAYER_POS_X];
    this.posY = (byte)evData[(byte)STATUS_PLAYER_POS_Y];
}
posted @ 2013-05-15 13:50  M守护神  阅读(774)  评论(0编辑  收藏  举报