PHOTON基础入门

来源:互联网 发布:成都景鑫云科技 知乎 编辑:程序博客网 时间:2024/06/05 11:24

首先去PhotonServer SDK下载服务器端SDK,需要登录的,就先注册一个账号吧.

解压出来是四个文件

deploy:主要存放photon的服务器控制程序和服务端Demo

doc:顾名思义,文档

lib:Photon类库,开发服务端需要引用的

src-server:服务端Demo源代码


今天搞一个客户端连接服务器最简单的程序,也算是hello world吧

客户端以Unity3d 为基础,hello world包括配置服务器,客户端,客户端连接服务器,客户端状态改变。

第一步:配置服务器端

打开deploy文件夹,看到包含了不同平台编译出的Photon目录,以“bin_”为前缀命名目录,选择你的电脑对应的文件夹打开,看到PhotonControl.exe,运行后,可以在windows右下角看到一个图标,点击图标可以看到photon服务器控制菜单,这个以后再做详细介绍.


打开visual stadio,新建项目,选择c# 类库,应用程序名字叫做MyServer.

完成后,把我们的Class1.cs,改名为MyApplication.cs,作为服务器端主类.然后在当前项目添加引用,链接到刚才提到的lib文件夹目录下,添加以下引用:

ExitGamesLibs.dll,

Photon.SocketServer.dll,

PhotonHostRuntimeInterfaces.dll

然后新建一个类:MyPeer.cs,写法如下:


  1. using System;    
  2. using System.Collections.Generic;   
  3. using System.Linq;
  4. using System.Text;
  5. using Photon.SocketServer;    
  6. using PhotonHostRuntimeInterfaces;    
  7.     
  8. namespace MyServer    
  9. {    
  10.     public class MyPeer : ClientPeer   
  11.     {    
  12.     
  13.         public  MyPeer(InitRequest initRequest)    
  14.             : base(initRequest)    
  15.         {     
  16.                 
  17.         }    
  18.     
  19.         protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)    
  20.         {    
  21.                 
  22.         }    
  23.     
  24.         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)    
  25.         {   
  26.     }    
  27.     }    
  28. }   
接上,MyApplication.cs类这样写:

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using Photon.SocketServer;    
  6.     
  7. namespace MyServer    
  8. {    
  9.     public class MyApplication : ApplicationBase    
  10.     {    
  11.     
  12.         protected override PeerBase CreatePeer(InitRequest initRequest)    
  13.         {    
  14.             return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);    
  15.         }    
  16.     
  17.         protected override void Setup()    
  18.         {    
  19.                 
  20.         }    
  21.     
  22.         protected override void TearDown()    
  23.         {    
  24.                 
  25.         }    
  26.     }    
  27. }   

完成后,在解决方案资源管理器中选中当前项目,打开属性,选择生成选项卡,把输出路径改成bin\,然后就生成类库吧

复制当前项目下MyServer文件夹到deploy文件夹下,删除除了bin文件夹以外其他所有文件和文件夹,然后文本编辑器打开deploy\bin_Win64\PhotonServer.config配置文件(我的是win7 64位机器,就选择这个),添加以下配置:

  1. <Application    
  2.                 Name="MyServer"    
  3.                 BaseDirectory="MyServer"    
  4.                 Assembly="MyServer"    
  5.                 Type="MyServer.MyApplication"    
  6.                 EnableAutoRestart="true"    
  7.                 WatchFiles="dll;config"    
  8.                 ExcludeFiles="log4net.config">   
  9. </Application> 
这段代码放在<Default><Applications>放这里</Applications></Default>节点下面

Name:项目名字

BaseDirectory:根目录,deploy文件夹下为基础目录

Assembly:是在生成的类库中的bin目录下与我们项目名称相同的.dll文件的名字

Type:是主类的全称,在这里是:MyServer.MyApplication,一定要包括命名空间

EnableAutoRestart:是否是自动启动,表示当我们替换服务器文件时候,不用停止服务器,替换后photon会自动加载文件

WatchFiles和ExcludeFiles

完成后保存,运行托盘程序deploy\bin_Win64\PhotonControl.exe,

(证书放在deploy\bin_Win64\目录下

下面开始编写客户端代码,首先从官网下载Unity SDK

打开Unity3d编辑器,首先把Photon-Unity3D_v3-0-1-14_SDK\libs\Release\Photon3Unity3D.dll导入到Unity中,新建脚本TestConnection.cs,脚本代码如下:

  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. using ExitGames.Client.Photon;    
  5.     
  6. public class TestConnection : MonoBehaviour,IPhotonPeerListener {    
  7.     public PhotonPeer peer;    
  8.     // Use this for initialization    
  9.     void Start () {    
  10.         peer = new PhotonPeer(this,ConnectionProtocol.Udp);    
  11.     }    
  12.         
  13.     // Update is called once per frame    
  14.     void Update () {    
  15.         peer.Service();    
  16.     }    
  17.         
  18.     void OnGUI(){    
  19.         if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){    
  20.             peer.Connect("localhost:5055","MyServer");    
  21.         }    
  22.     }    
  23.   
  24.     #region IPhotonPeerListener implementation    
  25.     public void DebugReturn (DebugLevel level, string message)    
  26.     {    
  27.             
  28.     }    
  29.     
  30.     public void OnOperationResponse (OperationResponse operationResponse)    
  31.     {    
  32.             
  33.     }    
  34.     
  35.     public void OnStatusChanged (StatusCode statusCode)    
  36.     {    
  37.         switch(statusCode){    
  38.         case StatusCode.Connect:    
  39.             Debug.Log("Connect Success!");    
  40.             break;    
  41.         case StatusCode.Disconnect:    
  42.             Debug.Log("Disconnect!");    
  43.             break;    
  44.         }    
  45.     }    
  46.     
  47.     public void OnEvent (EventData eventData)    
  48.     {    
  49.             
  50.     }    
  51.     #endregion    
  52. }   
把脚本绑定到场景中物体上,运行后可以看到一个按钮,点击连接,如果连接成功会打印"Connect Success!".

文章转载至http://751401909.iteye.com/blog/1987100

对peer.cs脚本进行了修改,因为新的photonServer的构造函数已经改变,与客户端通信的类需要继承clientPeer类,而不是之前的peerbase类,实际上clientPeer类继承的还是peerbase


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 亚马逊仓库满了怎么办 医保不给报销怎么办 海信电视特别卡怎么办 在花呗上被骗了怎么办 西洋杜鹃掉叶子怎么办 苹果iap支付失败怎么办 此项目无法退款怎么办 及贷逾期一天怎么办 快贷逾期一天怎么办 燃气灶开关松了怎么办 厨房插座挨灶台怎么办 天然气灶费电池怎么办 国美东西买贵啦怎么办 饥荒咕咕鸟死了怎么办 收到催天下信息怎么办 对门邻居有白事怎么办 顺丰理赔不合理怎么办 手机店抽奖被骗怎么办 手机店投票被骗怎么办 锤子手机进水了怎么办 锤子手机无法关机怎么办 坚果pro2卡顿怎么办 兴隆破产兴隆卡怎么办 电脑开机键失灵怎么办 网上买冰箱售后怎么办 物流公司损坏了怎么办 白色充电器线脏了怎么办 卷尺缩不回去怎么办 在昆山怎么办电瓶车牌 网购遇到质量问题怎么办 洗衣机外壳坏了怎么办 8元飞享套餐下线怎么办 改光纤后传真机怎么办 网上预约迟到了怎么办 国地税合并局长怎么办? 信用卡申请条件不足怎么办 深户落亲戚房产怎么办准迁证 从深圳迁出户口怎么办 快递未送达签收怎么办 床垫用水洗后怎么办 轿车空调不制冷怎么办