本文是关于Photon云存储初步学习方法:windows
工具下载连接:http://download.csdn.net/search?sort=&q=Photon&per_page=2服务器
在服务器文件夹中的deploy中是配置全部服务器的,安装好Photon后会看到有几个启动版本bin_Win32,bin_Win32_xp,根据本身的系统环境来选择。我系统是win10选的就是bin_Win64 里面有个PhotonControl.exe就是运行服务器。双击启动它。app
在你的系统右下角就会发现一个小圆圈,这个就是服务器啦!ide
右键它你会发现有个Photon instance:下面有个Default就是咱们要用的服务器啦函数
对了,下载下来的权限就放在这个bin文件夹,个人就是bin_Win64,弄完权限记得重启服务器啊。工具
下面咱们就来写一下服务器代码。一个简单的用户登陆学习
Photon用的C#咱们就用VS写,我用的是VS2015测试
首先咱们新建一个C#类库咱们叫MyServer,让咱们引入3个dll,在Photon的lib中this
ExitGamesLibs.dllspa
Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll
新建一个C#类咱们叫MyPeer,继承PeerBase,而后重写函数,别忘了using
<span style="font-size:14px;">using Photon.SocketServer; using PhotonHostRuntimeInterfaces; using MyServer.Message; using System.Collections; namespace MyServer { public class MyPeer : PeerBase { Hashtable userTabel; public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer) : base(protocol, photonPeer) { userTabel = new Hashtable(); userTabel.Add("145", "6734"); } protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { //失去连线时候要处理的事项,例如释放资源 } protected override void OnOperationRequest(OperationRequest operationRequest, SendParame ters sendParameters) { //取得Client端传过来的要求加以处理 switch (operationRequest.OperationCode) { case (byte)OpCodeEnum.Login: string uname = (string)operationRequest.Parameters[(byte)OpKeyEnum.UserName]; string pwd = (string)operationRequest.Parameters[(byte)OpKeyEnum.PassWord]; if (userTabel.ContainsKey(uname) && userTabel[uname].Equals(pwd)) { SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginSuccess, null), new SendParameters()); } else { SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginFailed, null), new SendParameters()); } break; } } } }</span>
而后咱们再建一个C#类叫MyApplication,咱们继承AppLicationBase,而后所有重写就好,每一个函数的意思我都给出注释了:
<span style="font-size:18px;">using Photon.SocketServer; namespace MyServer { public class MyApplication : ApplicationBase { protected override PeerBase CreatePeer(InitRequest initRequest) { //创建连线并回传给Photon Server return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer); } protected override void Setup() { //初始化GameServer } protected override void TearDown() { //关闭GameServer并释放资源 } } }</span>
还有个Message是用来标识状态的,一样新建一个C#类叫Message:
namespace MyServer.Message { enum OpCodeEnum : byte { Login = 1, LoginSuccess = 2, LoginFailed = 3, Create = 250, Join = 255, Leave = 254, RaiseEvent = 253, SetProperties = 252, GetProperties = 251 } enum OpKeyEnum : byte { RoomId = 251, UserName = 252, PassWord = 253 } }而后比较重要的一步,在VS中的解决方案中,咱们右键咱们的MyServer(C#类库名)打开属性,选择生成,把输出中的输出路径改成bin\,由于Photon就读取bin目录中的dll。
而后咱们就生成服务器就好啦~~~
而后把咱们的服务器MyServer中除了bin文件夹其余均可以删除,而后放到Photon中的deploy文件夹中,而后咱们来配置一下Photon
打开deploy目录中的bin目录,我就打开bin_Win64中的PhotonServer.config,用VS打开便可
建议阅读PhotonServer.config文件中的注释,不会英语的能够用有道。颇有帮助
咱们用的是Udp的传输方式,Photon只有一个接听端口就是5055,因此防火墙不要封这个端口还有843,是Unity和Flash的一个接通端口因此也不要封,防火墙不会开固定端口的见http://windows.microsoft.com/zh-cn/windows/open-port-windows-firewall#1TC=windows-7;
而后咱们要加一段代码在<Applications Default="Lite">下面:
<!-- MyServer Application --> <Application Name="MyServer" BaseDirectory="MyServer" Assembly="MyServer" Type="MyServer.MyApplication" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application>
而后保存便可。
这样咱们服务器端就配置完成了,如今让咱们打开Default中的Start as application,而后打开Open Logs 见到Server is running。。。表面服务器创建成功了。
而后就是Unity端了
咱们新建一个工程,而后引入一个dll直接拖到Unity中就行,Photon3Unity3D.dll 一样也在lib中。
让咱们建一个C# 脚本 叫hotonSocket,一样在引用中导入Photon3Unity3D.dll;
using UnityEngine; using ExitGames.Client.Photon; using System.Collections.Generic; public class PhotonSocket : MonoBehaviour,IPhotonPeerListener { #region 单例 private static PhotonSocket _Instance; public static PhotonSocket Instance { get { return _Instance; } } #endregion private string address; //最好在Awake或Start中赋值,Unity 小问题,容易形成值不更改,还有最好写成私有 private string Server; //同上 private PhotonPeer peer; public ClientState state; void Awake () { _Instance = this; address = "localhost:5055"; Server = "MyServer"; state = ClientState.DisConnect; peer = new PhotonPeer(this, ConnectionProtocol.Udp); peer.Connect(address, Server); } public void SendMessage(byte Code,Dictionary<byte,object> param) { peer.OpCustom(Code, param,true); } void Update () { peer.Service(); } public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { switch(operationResponse.OperationCode) { case (byte)OpCodeEnum.LoginSuccess: Debug.Log("login Success"); state = ClientState.LoginSuccess; break; case (byte)OpCodeEnum.LoginFailed: Debug.Log("login Failed"); state = ClientState.LoginFailed; break; } } public void OnStatusChanged(StatusCode statusCode) { switch(statusCode) { case StatusCode.Connect: Debug.Log("Connect"); break; case StatusCode.Disconnect: Debug.Log("DisConnect"); break; } } public enum ClientState : byte { DisConnect, Connect, LoginSuccess, LoginFailed } enum OpCodeEnum : byte { //Login Login = 1, LoginSuccess = 2, LoginFailed = 3, } }这样Unity的部分也完事了,就能够来测试啦,出现Connect的Debug就表面连接服务器成功,出现LoginSuccess就OK了。 好了就先写到这里,后续再添加,欢迎来喷,谢谢!!!