Operations are remote procedure calls with a request and a response. An operation request consists of an OperationCode and Parameters (a Dictionary<byte, object\> containing the parameters). To send operation requests to the server OpCustom of the peer instance is called. The operation result is returned to your application in the OnOperationResponse callback.Note:a) Sending the request and receiving the response are decoupled and fully asynchronous. b)the operation response is optional.Events are messages or notifications pushed to the client. When the peer receives an event the application is notified through the OnEvent callbackRooms group client connections or peers and facilitate the communication between peers. Peers can join a room and send events to all peers of the room or parts of them.OpJoin is the operation called by the client to join the room.OpRaiseEvent is the operation called to send events to other clients.
Note: Rooms, OpJoin and OpRaiseEvent are implemented in the Lite Application. The room concept is essential to many games because this is the approach many developers take - although it's not the only one - Photon is committed to an open architecture philosophy.
操作是一个远程过程调用,由一个请求和一个响应组成。一个操作请求包含一个OperationCode 和 参数(一个字典<字节,对象\ >包含了参数)。 操作请求发送到服务器peer实例的OpCustom被调用。OnOperationResponse回调时操作的结果被返回给你的应用程序中。
注意:a)发送请求和接收响应是解耦和完全异步的。b)操作响应是可选的。事件是消息或通知被推送到客户端。当peer收到一个事件时,应用程序将通过OnEvent回调得到通知。房间组织客户端的连接或peer并方便peer之间的交流。peer可以加入一个房间和发送事件到房间内的所有peer或其中部分。客户端加入房间时OpJoin操作被调用,将事件发送给其他客户端OpRaiseEvent操作被调用。注意:在Lite应用程序中实现房间,OpJoin和OpRaiseEvent。这房间的概念是至关重要的许多游戏,因为这是很多开发人员采取的方法——尽管它不是只有一个——Photon致力于一个开放式体系结构的理论研究。
Next we’ll refactor the code in main as follows
static
void
Main(
string
[] args)
{
new
Program().Run();
}
PhotonPeer peer;
public
Program()
{
peer =
new
PhotonPeer(
this
, ConnectionProtocol.Udp);
}
void
Run()
{
if
(peer.Connect(
"localhost:5055"
,
"Lite"
))
{
do
{
//Console.WriteLine(".");
peer.Service();
System.Threading.Thread.Sleep(500);
}
while
(!Console.KeyAvailable);
}
else
Console.WriteLine(
"Unknown hostname!"
);
Console.WriteLine(
"Press any key to end program!"
);
Console.ReadKey();
}
|
To start with the implementation of the flow we described above the first change to the code we’ll be to add a switch for the statusCode we get notified with and a case block for the StatusCode.Connect to send an OperationRequest to join a room with the name “MyRoomName” (lines 6-11):
public
void
OnStatusChanged(StatusCode statusCode)
{
Console.WriteLine(
"\n---OnStatusChanged:"
+ statusCode);
switch
(statusCode)
{
case
StatusCode.Connect:
Console.WriteLine(
"Calling OpJoin ..."
);
Dictionary<Byte, Object>opParams =
new
Dictionary<Byte,Object>();
opParams[(
byte
)LiteOpKey.GameId] =
"MyRoomName"
;
peer.OpCustom((
byte
)LiteOpCode.Join, opParams,
true
);
break
;
default
:
break
;
}
}
|
|
enum
OpCodeEnum :
byte
{
Join = 255,
Leave = 254,
RaiseEvent = 253,
SetProperties = 252,
GetProperties = 251
}
|
- 我们检查 operationResponse.ReturnCode 如果它失败了(不是0) 我们退出方法(行3-8)。
- 添加一个开关例子为OperationCode ( operationResponse.OperationCode ) LiteOpCode.Join (行11-13)
- 我们创建一个RaiseEvent操作请求( LiteOpCode.RaiseEvent )。参数LiteOpKey.Code= 101和LiteOpKey.Data= " Hello World”(我们的消息)(第18 - 21行)。
public
void
OnOperationResponse(OperationResponse operationResponse)
{
if
(operationResponse.ReturnCode == 0)
Console.WriteLine(
"\n---OnOperationResponse: OK - "
+ (OpCodeEnum)operationResponse.OperationCode +
"("
+ operationResponse.OperationCode +
")"
);
else
{
Console.WriteLine(
"\n---OnOperationResponse: NOK - "
+ (OpCodeEnum)operationResponse.OperationCode +
"("
+ operationResponse.OperationCode +
")\n ->ReturnCode="
+ operationResponse.ReturnCode +
" DebugMessage="
+ operationResponse.DebugMessage);
return
;
}
switch
(operationResponse.OperationCode)
{
case
(
byte
)LiteOpCode.Join:
int
myActorNr = (
int
)operationResponse.Parameters[LiteOpKey.ActorNr];
Console.WriteLine(
" ->My PlayerNr (or ActorNr) is:"
+ myActorNr);
Console.WriteLine(
"Calling OpRaiseEvent ..."
);
Dictionary<
byte
,
object
> opParams =
new
Dictionary<
byte
,
object
>();
opParams[LiteOpKey.Code] = (
byte
)101;
opParams[LiteOpKey.Data] =
"Hello World!"
;
peer.OpCustom((
byte
)LiteOpCode.RaiseEvent, opParams,
true
);
break
;
}
}
|
C:\...\HelloWorld2\bin\Debug>HelloWorld2.exe---OnStatusChanged:ConnectCalling OpJoin ...---OnOperationResponse: OK - Join(90)->My PlayerNr (or ActorNr) is:1Calling OpRaiseEvent ...---OnOperationResponse: NOK - RaiseEvent(253)->ReturnCode=-1 DebugMessage=Wrong parameter type 245(RaiseEventRequest.Data): should be Hashtable but received String
switch
(operationResponse.OperationCode)
{
case
LiteOpCode.Join:
int
myActorNr = (
int
)operationResponse.Parameters[LiteOpKey.ActorNr];
Console.WriteLine(
" ->My PlayerNr (or ActorNr) is:"
+ myActorNr);
Console.WriteLine(
"Calling OpRaiseEvent ..."
);
Dictionary<
byte
,
object
> opParams =
new
Dictionary<
byte
,
object
>();
opParams[LiteOpKey.Code] = (
byte
)101;
//opParams[LiteOpKey.Data] = "Hello World!"; //<- returns an error, server expects a hashtable
Hashtable evData =
new
Hashtable();
evData[(
byte
)1] =
"Hello Wolrd!"
;
opParams[LiteOpKey.Data] = evData;
peer.OpCustom((
byte
)LiteOpCode.RaiseEvent, opParams,
true
);
break
;
}
|
enum
EvCodeEnum :
byte
{
Join = 255,
Leave = 254,
PropertiesChanged = 253
}
|
public
void
OnEvent(EventData eventData)
{
Console.WriteLine(
"\n---OnEvent: "
+ (EvCodeEnum)eventData.Code +
"("
+ eventData.Code +
")"
);
switch
(eventData.Code)
{
case
101:
int
sourceActorNr = (
int
)eventData.Parameters[LiteEventKey.ActorNr];
Hashtable evData = (Hashtable)eventData.Parameters[LiteEventKey.Data];
Console.WriteLine(
" ->Player"
+ sourceActorNr +
" say's: "
+ evData[(
byte
)1]);
break
;
}
}
|
the first client: 第一个客户端
C:\...\HelloWorld2\bin\Debug>HelloWorld2.exe---OnStatusChanged:ConnectCalling OpJoin ...---OnOperationResponse: OK - Join(255)->My PlayerNr (or ActorNr) is:1Calling OpRaiseEvent ...---OnEvent: Join(255)->Player1 joined!->Total num players in room:1, Actornr List: 1,---OnEvent: Join(255)->Player2 joined!->Total num players in room:2, Actornr List: 1,2,---OnEvent: 101(101)->Player2 say's: Hello Wolrd!
the second client: 第二个客户端
C:\...\HelloWorld2\bin\Debug>HelloWorld2.exe---OnStatusChanged:ConnectCalling OpJoin ...---OnOperationResponse: OK - Join(255)->My PlayerNr (or ActorNr) is:2Calling OpRaiseEvent ...---OnEvent: Join(255)->Player2 joined!->Total num players in room:2, Actornr List: 1,2,
- join事件被自己的加入所触发。
- join事件被第二个加入的客户端所触发。
- 101事件,第二个客户端发送给房间里的所有其他人。
|