- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
Here we willgive you a quick overview of the basic concepts for the Lite application.
Peers
Lite is based onthe Application framework for Photon and also uses “Peer” as reference to aconnected player. This is wrapped up and extended in the class LitePeer.
Lite是基于Photon的Application框架而设计的,使用Peer来代表连接的用户.Peer的封装和扩展在LitePeer类中实现.When a clientconnects 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 thecorresponding LitePeer.
当客户端向Lite Application发起一个连接时,一个新的LitePeer对象就由LiteApplication.CreatePeer创建了.In Lite, everyLitePeer has a single RoomReference as State. Players can be only in a singleroom. The State is set by operation Join.
在Lite里每一个LitePeer都有一个RoomReference作为State.玩家只可能在一个房间(Room)中.State由Join操作来设置.The OnDisconnectmethod in LitePeer is called when a peer disconnects. If the peer is still in aroom, this gets a message to remove the peer.
当peer失去连接后LitePeer的OnDisconnect方法就被调用了.如果某个peer仍在在房间中,房间将会收到一个消息来移除此peer.Rooms房间In Lite, peersget into rooms to play together, which is why they are also called games. Roomsare responsible for everything. Everything aside from join, that’s it.
在Lite中,众多peer是在不同的房间中运行的,因此房间也被叫做” 游戏”.房间负责除了” 加入(join)”以外的所有事务.Every game has aunique name and is treated as completely separated from any other. Also, everyrequest that comes from a player is handled in sequence. Because many room***ist in parallel and each request is handled in mere ticks, this setup scaleswell.
每一个game都有一个唯一的名字来彼此区分.所有来自玩家的请求都依次按顺序处理,因为所有的游戏房间都是平行存在的,而且每一个请求都在瞬间处理完,此法令系统可以具有良好的伸缩性.Rooms have listsof players which they update on join, leave or disconnect. Between theseplayers, events can be sent.
房间中还保存有玩家的列表,在他们加入时插入离开时移除.玩家间彼此可以通过event(事件)来进行通讯.Within a room, aplayer is also known as Actor and each Actor has an ActorNumber. That number isused to identify the origin of an event.
在房间里,每个玩家用一个Actor类来表示,每一个Actor实例都有一个ActorNumer属性,这个属性可以用于区分每个事件的发送者.
Operations and Events操作与事件Lite definesthese Operations
Lite中定义了如下几种操作类:
•Join: Enter any room by name. It gets created on the fly if it does notexist. Join will implicitly leave the previous room of a peer (if any). Returnsthe assigned ActorNumber.加入进入:使用任意名称来进入房间.如果这个房间名不存在则创建,如果名字已经存在则加入这个房间.每当进入到一个新房间的时候Join操作都会自动离开上一个房间.进入房间后将会分配到一个ActorNumber.
•Leave: Leaves the room (but keeps the connection).离开:离开房间,但是保持与服务器的连接.
•RaiseEvent: Tells the room to send an event to the other peers in it. Eventshave an EventCode and can carry data provided by the client. Lite does notstore events.发起事件:告诉room给房间中其他的peer发送事件.事件包含一个EventCode(事件代码),并且在事件中携带需要通讯的数据.Lite本身并不保存这些事件.
•GetProperties: In Lite, properties can be attached to rooms and players. With thisoperation, they are fetched.获得属性:在Lite中,属性可以附加到房间和玩家上.通过GetProperties操作可以获取这些属性.
•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. Codeby convention.设置属性:将任意键值对赋到一个房间或者一个玩家上.Lite没有使用这些数据,所以客户端可以使用代码发送任意的数据.
Lite definesthese Events
Lite定义了如下事件•·Join: Joining a room (by Operation) updates the players list inside. Thisis sent in an event to all players (including the new one).进入加入:通过Operation(操作)进入房间并更新房间中的玩家列表.所有玩家(包括这个刚加入的玩家)将通过事件收到这个更新后的玩家列表.
•·Leave: If a player leaves a room, the others are updated again.离开:同理如果一个玩家离开了房间,其他玩家也将收到此更新.
•·Propertiesupdate: When properties are changed, there isthe option to broadcast the change. This event takes care that every client isup to date.属性更新:这个一个将属性更新通过广播更新给其他玩家的方法.属性更新事件保持着每一个客户端的属性都是最新的.
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 theevent content accordingly.
在Lite中玩家可以给同一个房间内的其他用户发送任意内容.RaiseEvent操作就是用来做这事的.
As an example,this client code from the DotNet Realtime Demo sends a player’s position:
以下是一个示范,这段来自.net 实时的Demo演示的是发送玩家位置:// Raises an event with the position data of thisplayer.发起一条含有玩家位置信息的事件
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 LiteApplication does not try to interpret your events server-side, so you can sendjust about anything you want to. The EV_MOVE above is nothing Lite knows, it’sonly defined and used client-side.
Lite Application不会在服务端里打断你的事件,所以你可以发送你想要发送的数据.上面的EV_MOVE是Lite里没有定义的东西,它只在客户端定义和使用.
The move eventthat’s raised in the sample above causes a EventAction call onthe receiving clients. They handle the event and data like this:
上面发起的移动事件在接收端触发了一个EventAction的调用,这个调用用来处理事件和数据,如下所示//in Game.cs:
public void EventAction(byte eventCode, HashtablephotonEvent)
{
int actorNr = 0;
if (photonEvent.ContainsKey((byte)LiteEventKey.ActorNr))
{
actorNr= (int) photonEvent[(byte) LiteEventKey.ActorNr];
}
// get the player that raisedthis 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];
}Lite Lobby Concepts
Lite Lobby相关概念http://doc.exitgames.com/photon-server//LiteLobbyConcepts#cat-Application - Lite & Lite Lobby Lite Lobby is an application that (literally) extends the LiteApplication to offer a listing of currently used rooms. To achieve this, itimplements two different types of rooms: the LiteLobbyRoom and a LiteLobbyGame.
Lite Lobby从字面上就可以看出是一个对Lite Application进行扩展的应用,额外提供了当前可用房间的一个列表.为了实现这样的功能,Lite Lobby实现了两种不同类型的房间: LiteLobbyRoom 和 LiteLobbyGame.
By convention, join will put you into a lobby when the givenroom name ends on “_lobby” and into a game room in all other cases. Theoperation join got an optional parameter to name a lobby where the game islisted. This puts the responsibility on the client side again and you cancreate lobbies on the fly.
当你传入一个以” _lobby”结尾的房间名的时候,join(加入)会把你放进一个大厅(lobby)中,而其他时候你会进到一个游戏房间.join操作会得到一个可选的参数来命名一个用于陈列游戏房间列表的大厅.建立大厅又是客户端的责任了,你可以通过这种方式来任意的创建大厅.Lobby大厅The lobby rooms (LiteLobbyRoom.cs) are special as they are notused to play a match with opponents. They just provide a list of availablerooms (games) instead, so player can choose and join those.
大厅(在LiteLobbyRoom.cs中)的特别之处是它不是用来进行游戏的地方.它仅仅是提供一个可用的游戏房间的列表,玩家可以通过这个列表来进入到不通的游戏房间中去进行游戏.
Despite being a room, too, a lobby won’t send events on join andleave of actors. As a result, players inside a lobby don’t notice each otherwhich prevents a flood of join/leave events. There are no operations aside fromleave. As a player leaves rooms when joining another, leave is not evenrequired.
尽管大厅也是一个房间,但是它不会发送玩家的加入和离开事件.其结果是大厅中的玩家就不会收到如洪水般汹涌频繁的加入与离开事件了.除了”离开”,lobby中就没有其他操作提供了.因为玩家加入另一个房间时候会离开当前房间,所以”离开”并不是必须的.
On join, the complete games list is sent once to get a client upto date. After that, each player gets the same update event, which is sentevery few seconds. It lists each game by name its current number of players.The clients keep and update their initial list. Games that are empty or full,both simply report 0 players as they should no longer be in the room list.
加入大厅的时候客户端会收到一份最新的完整游戏房间列表.此后,每个玩家每隔几秒钟都会收到一个同样的更新事件.列表中有每个房间的名字和其中的玩家数量.客户端保存并更新他们的初始列表.游戏房间不是空就是满,一旦房间中的玩家数量为0,这个房间将不会再出现在房间列表里.
To send the room list in intervals, the lobby schedules amessage for itself, once the list is sent. This enqueues the event sending inthe regular message passing mechanism, avoiding threading issues.
房间列表会间隔性的发送.这些要发送的事件会放在一个消息队列中,以免发生线程问题.Rooms for Games游戏房间In Lite Lobby, regular games can now be connected to a lobby.The lobby of a room is set when gets created. If the lobby does not exist, itwill be created as well.
在Lite Lobby中,正常的游戏房间就会被连接到一个大厅里.一旦房间被创建,大厅也会被更新.即使此时大厅不存在,系统也会自动创建一个.
Once a room is connected to a lobby it is responsible to updatethe lobby with its info. The lobby itself does not mind what’s forwarded asinfo to the clients. By default, each room only sends its actor count and willbe “full” when 4 players are in a room.
一旦房间连接到大厅,他就会用他的信息来更新大厅数据.大厅并不关心什么样的信息会传递给客户端.默认情况下,每个房间只传递其中的玩家数量,并且当人数到达4个人的时候标识为满员状态.LiteLobby ConfigLiteLobby配置To avoid incompatibilities, Lite Lobby uses its applicationconfig file to define the event types, update interval and the lobby suffix.
为了避免不一致,LiteLobby使用应用配置文件来定义他的事件类型更新间隔和大厅名字识别后缀(默认的是” _lobby”).
Some values can’t be edited this way yet: count of users to makea room “full”, event-values and more.
目前一些数值还不能通过配置文件的方式来配置:房间满员的上限事件值以及其他.
Lite Lobby can be ***nning parallel to Lite but usually a gamewould use just one of those. The SDKs Photon Config assigns the name Lite Lobby(used by the clients on connect).
理论上LiteLobby可以同时运行多个实例,但是一般情况下一个游戏应该只有一个大厅.SDK中的PhotonConfig规定了客户端用来连接的大厅的名字.Extending Lite扩展Lite
http://doc.exitgames.com/photon-server//ExtendingLite#cat-Application - Lite & Lite Lobby
Persistency is currently not covered in the SDKs we provide. Noneof our applications saves any data. Every game and application is different andon a high performance server solution, you probably want to control this aspectyourself.
目前的Photon SDK中没有提供持久化的功能.我们的程序不能保存任何数据.但是每一个游戏和程序都是不同的,在一个高性能的服务器方案中你可能会需要对这些方面进行控制.
You are free to use any of the many professional solutionsdeveloped in C#. As example, take a look at:
你可以自由无阻的使用C#来进行更多专业功能的开发.例如你可能会用到:
·NHibernate:Mature, open source object-relational mapper for the .NET framework一个成熟的开源对象-关系映射框架·Membaseistributed key-value database management system一个键值数据库管理系统·CSharp-SQLite: SQL database in a local file一个本地SQL数据库·Lightspeed: High performance .NET domainmodeling and O/R mapping framework一个高性能的.net领域建模和对象-关系映射框架Trigger Game-Logic In Intervals定时触发游戏逻辑If you want yourserver application to ***cute some logic in intervals, add this as method intoLiteGame and schedule a message for the room. By using a message, the methodcall will be in sequence with any operations (avoiding threading issues).
如果你想在你的游戏中间隔性的执行一些游戏逻辑,可以将以下方法添加到LiteGame中,这让游戏房间可以定时的发送消息.通过消息的方式,这个方法可以用任何操作来有序调用(避免线程问题).
In the Lite LobbyApplication’s LiteLobbyRoom.cs we used this code:
在LiteLobby应用里的LiteLobbyRoom.cs里加上这段代码
/// <summary>Schedules a broadcast of all changes.定时广播所有更新</summary>
private void SchedulePublishChanges()
{
var message = new RoomMessage((byte)LobbyMessageCode.PublishChangeList);
this.schedule = this.ScheduleMessage(message,LobbySettings.Default.LobbyUpdateIntervalMs);
}/// <summary>Initializes a new instance of the LiteLobbyRoomclass.初始化一个LiteLobbyRoom的新实例</summary>
public LiteLobbyRoom(string lobbyName)
: base(lobbyName)
{
this.roomList = new Hashtable();
this.changedRoomList = new Hashtable(); // schedule sending the change list
this.SchedulePublishChanges();
}/// <summary>Sends the change list to all users in the lobbyand then clears it.发送大厅中所有用户的变化列表然后清除之</summary>
private void PublishChangeList()
{
//do something in intervals... //schedule the next call!
this.SchedulePublishChanges();
} 本文转载于unity3d圣典 更多分享尽在Web3纳金网http://www.narkii.com/ |
|