Unity3d:使用photon sever 自建服务器

第一步:下载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类

  1.   class ChatServer : ApplicationBase
  2.     {
  3.         //客户端连接时调用
  4.         protected override PeerBase CreatePeer(InitRequest initRequest)
  5.         {
  6.             return new ChatPeer(initRequest.Protocol,initRequest.PhotonPeer);
  7.         }
  8.         //客户端启动时调用
  9.         protected override void Setup()
  10.         {
  11.             
  12.         }
  13.         //客户端被调用时调用
  14.         protected override void TearDown()
  15.         {
  16.             
  17.         }
  18.     }

处理客户端请求的类,继承自PeerBase类,为每一个客户端在连接时创建

  1.     class ChatPeer : PeerBase
  2.     {
  3.         //和客户端交互
  4.         public ChatPeer(IRpcProtocol protocol, IPhotonPeer peer):base(protocol,peer)
  5.         {
  6.         }
  7.         protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
  8.         {
  9.             
  10.         }
  11.         //响应客户端请求
  12.         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
  13.         {
  14.             //使用字典存储参数
  15.             Dictionary<byte, object> dic = new Dictionary<byte, object>();
  16.             dic.Add(1,"user001");
  17.  
  18.             OperationResponse operationResponse = new OperationResponse(1,dic);
  19.             //发送请求
  20.             SendOperationResponse(operationResponse,sendParameters);
  21.         }
  22.     }
  23. }
  24. 鼠标右键生成

Unity客户端:导入Photon3Unity3D.dll,在相机上挂在ChatClient脚本

客户段类,实现监听接口IPhotonPeerlistener

  1. public class ChaClient : MonoBehaviour,IPhotonPeerListener {
  2.     //打印日志
  3.     public void DebugReturn(DebugLevel level, string message)
  4.     {
  5.         print(level+":::"+message);
  6.     }
  7.     public void OnEvent(EventData eventData)
  8.     {
  9.         
  10.     }
  11.     //接收服务器端的返回
  12.     public void OnOperationResponse(OperationResponse operationResponse)
  13.     {
  14.         //使用字典存储
  15.         Dictionary<byte, object> dic = operationResponse.Parameters;
  16.         object v = null;
  17.         //获取参数
  18.         dic.TryGetValue(1,out v);
  19.         print("Get Value from Server:"+v.ToString());
  20.     }
  21.     //状态改变
  22.     public void OnStatusChanged(StatusCode statusCode)
  23.     {
  24.         switch (statusCode)
  25.         {
  26.             //连接成功
  27.             case StatusCode.Connect:
  28.                 isConnected = true;
  29.                 break;
  30.              //断开连接
  31.             case StatusCode.Disconnect:
  32.                 break;
  33.             //异常
  34.             case StatusCode.Exception:
  35.                 break;
  36.             case StatusCode.ExceptionOnConnect:
  37.                 break;
  38.             case StatusCode.SecurityExceptionOnConnect:
  39.                 break;
  40.             case StatusCode.QueueOutgoingReliableWarning:
  41.                 break;
  42.             case StatusCode.QueueOutgoingUnreliableWarning:
  43.                 break;
  44.             case StatusCode.SendError:
  45.                 break;
  46.             case StatusCode.QueueOutgoingAcksWarning:
  47.                 break;
  48.             case StatusCode.QueueIncomingReliableWarning:
  49.                 break;
  50.             case StatusCode.QueueIncomingUnreliableWarning:
  51.                 break;
  52.             case StatusCode.QueueSentWarning:
  53.                 break;
  54.             case StatusCode.InternalReceiveException:
  55.                 break;
  56.             case StatusCode.TimeoutDisconnect:
  57.                 break;
  58.             case StatusCode.DisconnectByServer:
  59.                 break;
  60.             case StatusCode.DisconnectByServerUserLimit:
  61.                 break;
  62.             case StatusCode.DisconnectByServerLogic:
  63.                 break;
  64.             case StatusCode.TcpRouterResponseOk:
  65.                 break;
  66.             case StatusCode.TcpRouterResponseNodeIdUnknown:
  67.                 break;
  68.             case StatusCode.TcpRouterResponseEndpointUnknown:
  69.                 break;
  70.             case StatusCode.TcpRouterResponseNodeNotReady:
  71.                 break;
  72.             case StatusCode.EncryptionEstablished:
  73.                 break;
  74.             case StatusCode.EncryptionFailedToEstablish:
  75.                 break;
  76.             default:
  77.                 break;
  78.         }
  79.     }
  80.   
  81.    //连接的服务器,本机
  82.     private string ip = "127.0.0.1:4530";
  83.     //服务器名
  84.     private string servername = "ChatServer";
  85.     //是否连接成功
  86.     public bool isConnected = false;
  87.     //服务器连接器
  88.     private PhotonPeer peer;
  89.  
  90.     void Start () {
  91.         //与服务器交互                      当前类           TCP协议
  92.          peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
  93.         //连接服务器
  94.         peer.Connect(ip,servername);
  95.         print("Connecting...");
  96.  
  97.     }
  98.  
  99.     void Update () {
  100.         //请求服务器的连接
  101.         if(peer!=null){
  102.         peer.Service();
  103.      }
  104.     }
  105. //创建一个button,点击连接
  106.     void OnGUI()
  107.     {
  108.         if (isConnected)
  109.         {
  110.             if (GUILayout.Button("发起请求"))
  111.             {
  112.                 Dictionary<byte, object> dic = new Dictionary<byte, object>();
  113.                 dic.Add(1,"username");
  114.                 dic.Add(2,"password");
  115.                 peer.OpCustom(1,dic,true);
  116.             }
  117.         }
  118.     }
  119.  
  120. }

第三部:服务器的配置

打开PhotonServer.config文件在<Default><Applications>添加下面的配置</Applications></Default>

  1.  <Application
  2.                 Name="ChatServer"    //服务器名字,
  3.                 BaseDirectory="ChatServer"   //deploy目录下的ChatServer文件夹
  4.                 Assembly="ChatServer"            //ChatSeverver文件夹下的ChatServer.dll
  5.                 Type="ChatServer.ChatServer"  //服务器启动的类,ChatServer.dll中的ChatServer:ApplicationBase类
  6.                 ForceAutoRestart="true"
  7.                 WatchFiles="dll;config"
  8.                 ExcludeFiles="log4net.config">  //日志
  9.             </Application>

启动服务器,右键,default 启动,运行客户端

点击button就可以接收到服务器端发来的参数user001;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值