1:实现IPhotonPeerListener接口用来接收消息。
接口中有如下4个方法:
摘要:当photon服务器派发事件到客户端时被调用。
void OnEvent(EventData eventData);
摘要:Callback method which gives you (async) responses for called operations.
void OnOperationResponse(OperationResponse operationResponse);
摘要: OnStatusChanged is called to let the game know when asyncronous actions finished or when errors happen.
public
void
OnStatusChanged(StatusCode statusCode)
2.Main Part:
创建一个listener实例 :var listener =
new
Program();
创建一个photonPeer实例 :var peer =
new
PhotonPeer(listener, ConnectionProtocol.Udp);
连接photon服务器peer.Connect("localhost:5055"
,
"Lite"
);
调用peer.Service();触发网络传输,派发消息。
3.Operations :通过请求(request)和响应(response)调用远程程序,它是我们的术语远程调用。
就是在服务器端实现的方法在客户端被调用。operations是运行在服务器端的应用程序。
Events :发送到客户端的消息和通知。和operation不同的是,events来自外部,服务器或者是其他客户端。
客户端通过OnEvent回调函数响应。例如有玩家加入房间,该玩家会发送一个请求使用OpCustom方法来调用operation,并且使用一个Dictionary来传递参数。
peer.OpCustom((
byte
)LiteOpCode.RaiseEvent, opParams,
true
);
房间里面的其他玩家会
Dictionary<Byte, Object>opParams =
new
Dictionary<Byte,Object>();
opParams[(
byte
)LiteOpKey.GameId] =
"MyRoomName"
;
peer.OpCustom((
byte
)LiteOpCode.Join, opParams,
true
);
注释:
当statuscode == StatusCode.Connect 时调用peer.Opcustom。
创建一个hashtable(opParams),里面包含了参数。调用peer.OpCustom,参数(OperationCode和OperationParams)。