第一步:下载photon sever,安装
deploy目录:存放要启动的程序
bin_tools 工具类
bin_Win32/64 根据自己自己电脑选择
PhotonControl.exe服务器启动程序
PhotonServer.config服务器启动配置文件
doc目录:存放文档
lib目录:存放类库
scr-server目录:存放源码
第二部:创建自己的服务器端,用vs创建一个C#的类库工程,引入类库ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll
输出路径为deploy/ChatServer/bin文件下
开始编写代码:服务器端代码
服务器启动时的主类,继承自ApplicationBase类
- class ChatServer : ApplicationBase
- {
- //客户端连接时调用
- protected override PeerBase CreatePeer(InitRequest initRequest)
- {
- return new ChatPeer(initRequest.Protocol,initRequest.PhotonPeer);
- }
- //客户端启动时调用
- protected override void Setup()
- {
- }
- //客户端被调用时调用
- protected override void TearDown()
- {
- }
- }
处理客户端请求的类,继承自PeerBase类,为每一个客户端在连接时创建
- class ChatPeer : PeerBase
- {
- //和客户端交互
- public ChatPeer(IRpcProtocol protocol, IPhotonPeer peer):base(protocol,peer)
- {
- }
- protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
- {
- }
- //响应客户端请求
- protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
- {
- //使用字典存储参数
- Dictionary<byte, object> dic = new Dictionary<byte, object>();
- dic.Add(1,"user001");
- OperationResponse operationResponse = new OperationResponse(1,dic);
- //发送请求
- SendOperationResponse(operationResponse,sendParameters);
- }
- }
- }
- 鼠标右键生成
Unity客户端:导入Photon3Unity3D.dll,在相机上挂在ChatClient脚本
客户段类,实现监听接口IPhotonPeerlistener
- public class ChaClient : MonoBehaviour,IPhotonPeerListener {
- //打印日志
- public void DebugReturn(DebugLevel level, string message)
- {
- print(level+":::"+message);
- }
- public void OnEvent(EventData eventData)
- {
- }
- //接收服务器端的返回
- public void OnOperationResponse(OperationResponse operationResponse)
- {
- //使用字典存储
- Dictionary<byte, object> dic = operationResponse.Parameters;
- object v = null;
- //获取参数
- dic.TryGetValue(1,out v);
- print("Get Value from Server:"+v.ToString());
- }
- //状态改变
- public void OnStatusChanged(StatusCode statusCode)
- {
- switch (statusCode)
- {
- //连接成功
- case StatusCode.Connect:
- isConnected = true;
- break;
- //断开连接
- case StatusCode.Disconnect:
- break;
- //异常
- case StatusCode.Exception:
- break;
- case StatusCode.ExceptionOnConnect:
- break;
- case StatusCode.SecurityExceptionOnConnect:
- break;
- case StatusCode.QueueOutgoingReliableWarning:
- break;
- case StatusCode.QueueOutgoingUnreliableWarning:
- break;
- case StatusCode.SendError:
- break;
- case StatusCode.QueueOutgoingAcksWarning:
- break;
- case StatusCode.QueueIncomingReliableWarning:
- break;
- case StatusCode.QueueIncomingUnreliableWarning:
- break;
- case StatusCode.QueueSentWarning:
- break;
- case StatusCode.InternalReceiveException:
- break;
- case StatusCode.TimeoutDisconnect:
- break;
- case StatusCode.DisconnectByServer:
- break;
- case StatusCode.DisconnectByServerUserLimit:
- break;
- case StatusCode.DisconnectByServerLogic:
- break;
- case StatusCode.TcpRouterResponseOk:
- break;
- case StatusCode.TcpRouterResponseNodeIdUnknown:
- break;
- case StatusCode.TcpRouterResponseEndpointUnknown:
- break;
- case StatusCode.TcpRouterResponseNodeNotReady:
- break;
- case StatusCode.EncryptionEstablished:
- break;
- case StatusCode.EncryptionFailedToEstablish:
- break;
- default:
- break;
- }
- }
- //连接的服务器,本机
- private string ip = "127.0.0.1:4530";
- //服务器名
- private string servername = "ChatServer";
- //是否连接成功
- public bool isConnected = false;
- //服务器连接器
- private PhotonPeer peer;
- void Start () {
- //与服务器交互 当前类 TCP协议
- peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
- //连接服务器
- peer.Connect(ip,servername);
- print("Connecting...");
- }
- void Update () {
- //请求服务器的连接
- if(peer!=null){
- peer.Service();
- }
- }
- //创建一个button,点击连接
- void OnGUI()
- {
- if (isConnected)
- {
- if (GUILayout.Button("发起请求"))
- {
- Dictionary<byte, object> dic = new Dictionary<byte, object>();
- dic.Add(1,"username");
- dic.Add(2,"password");
- peer.OpCustom(1,dic,true);
- }
- }
- }
- }
第三部:服务器的配置
打开PhotonServer.config文件在<Default><Applications>添加下面的配置</Applications></Default>
- <Application
- Name="ChatServer" //服务器名字,
- BaseDirectory="ChatServer" //deploy目录下的ChatServer文件夹
- Assembly="ChatServer" //ChatSeverver文件夹下的ChatServer.dll
- Type="ChatServer.ChatServer" //服务器启动的类,ChatServer.dll中的ChatServer:ApplicationBase类
- ForceAutoRestart="true"
- WatchFiles="dll;config"
- ExcludeFiles="log4net.config"> //日志
- </Application>
启动服务器,右键,default 启动,运行客户端
点击button就可以接收到服务器端发来的参数user001;