Unity连Photon服务器入门详解

Photon是目前比较好用的游戏服务器。目前网上对于Photon的服务器讲解比较少,最近也对Photon做了初步的了解,做一个极其详细的入门。

首先就是得下载Photon咯。

这个是服务器下载 :https://www.photonengine.com/en/OnPremise/Download (得注册账号)

这个服务器是有权限的,免费的有100人上限和30天免费,调试用的话,100人的还是很合算的。

这里有个比较好的教程 http://download.csdn.net/download/a762923309/4936547 是免费的,下载就好(CSDN的)

如果你能看懂教程就可以了。

 

现在开始正题。

在服务器的deploy中是配置所有服务器的,会看到有几个启动版本bin_Win32,bin_Win32_xp,根据自己的系统环境来选择。

我系统是win10选的就是bin_Win64 里面有个PhotonControl.exe就是运行服务器。双击启动它。

在你的系统右下角就会发现一个小圆圈,这个就是服务器啦!

右键它你会发现有个Photon instance:下面有个Default就是我们要用的服务器啦,上面的教程中这里是不同的,不过没差多少。

对了,下载下来的权限就放在这个bin文件夹,我的就是bin_Win64,弄完权限记得重启服务器啊。

下面我们就来写一下服务器代码。一个简单的用户登录

Photon用的C#我们就用VS写,我用的是VS2015

首先我们新建一个C#类库我们叫MyServer,让我们引入3个dll,在Photon的lib中

ExitGamesLibs.dll

Photon.SocketServer.dll

PhotonHostRuntimeInterfaces.dll

新建一个C#类我们叫MyPeer,继承PeerBase,然后重写函数,别忘了using

 

using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace MyServer
{
    using Message;
    using System.Collections;

    public class MyPeer : PeerBase
    {

        Hashtable userTabel;

        public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer) : base(protocol, photonPeer)
        {
            userTabel = new Hashtable();
            userTabel.Add("123", "1234");
        }

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            //失去连线时候要处理的事项,例如释放资源
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters 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;
            }
        }
    }
}
View Code

然后我们再建一个C#类叫MyApplication,我们继承AppLicationBase,然后全部重写就好,每个函数的意思我都给出来了

 

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并释放资源
        }
    }
}
View Code

还有个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
    }
}
View Code

 然后比较重要的一步,在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# Script 叫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,
    }
}
View Code

这样Unity的部分也完事了,就可以来测试啦,出现Connect的Debug就表面链接服务器成功,出现LoginSuccess就OK了。

以上的都是我们在教程中都能找到的部分,然后我们来说说教程中没有的Unity部分的address。

如果想局域网联机的就要找到你的本机内网IP,然后让address = 本机IP:5055 这样就OK了,

如果要想要外网链接呢有两种情况,一种你是用路由器的,也有两种方式,一种开DMZ主机,一种开虚拟服务器。

第一种DMZ主机,不建议用这种方式,他会把你的IP完全暴露在外网,不安全, address = 外网IP:5055

第二种虚拟服务器,这种方式就是开放部分端口,比较高端的路由器可以设置端口对端口,不高端的路由器只能指定端口,address = WAN口IP:5055

没有服务器的呢,下一个花生壳软件,他会给你一个免费的域名,然后挂在你的外网IP上然后 address = 花生壳域名:5055

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值