【unity服务器配置】PhotonServer2017搭建最新配置教程,Unity亲测完美运行!

最近题主在研究PhotonServer如何实现联网功能时,在搭建服务器与客户端时遇到了许多小问题,题主在网上参照了siki老师的photonServer搭建教程,但是遇到的问题却很多,原因是siki老师所讲的版本是几年前的版本,所以跟现在的新版本是有不同之处的,也由于题主没有网络通信的基础,所以一直折腾了我不少时间,现在终于搞定了整个流程,现在我来为大家分享一下2017年最新的PhotonServer搭建教程。

客户端 GameDemoChatClient

using ExitGames.Client.Photon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameDemoChatClient                   //客户端
{

    class ChatServerListener : IPhotonPeerListener
    {
        public bool isConnected = false;

        public void DebugReturn(DebugLevel level, string message)
        {

        }

        public void OnEvent(EventData eventData)      //返回给客户端时调用
        {
            //throw new NotImplementedException();
        }
        public void OnOperationResponse(OperationResponse operationResponse)     //得到服务器端的响应
        {

            Dictionary<byte, object> dict = operationResponse.Parameters;
            object v = null;
            dict.TryGetValue(1,out v);
            if (v == null)
            {

                Console.WriteLine("error code:" + operationResponse.ReturnCode + "     error message:" + operationResponse.DebugMessage);
            }
            else
            {
                Console.WriteLine("从服务端得到数据:" + v.ToString());
            }


        }

        public void OnMessage(object messages)
        {

        }

        public void OnStatusChanged(StatusCode statusCode)      //客户端连接服务端成功时调用
        {
            switch (statusCode)
            {
                case StatusCode.Connect:
                    isConnected = true;
                    Console.WriteLine("连接成功!");
                    break;       
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
         string localhost = "127.0.0.1:4530";
         string ChatServer = "ChatServer";
        ChatServerListener listener = new ChatServerListener();
            PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Tcp);
            peer.Connect(localhost, ChatServer);     //链接服务器
            Console.WriteLine("连接服务器中......");
            while (listener.isConnected == false)
            {
                peer.Service();
            }
            Dictionary<byte, object> dict = new Dictionary<byte, object>();
            dict.Add(1,"username");
            dict.Add(2, "password");
            //向服务端发送请求
            peer.OpCustom(1,dict,true);
            while (true) {
             peer.Service();
            }
        }
       }
    }

服务端:ChatPeer

using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PhotonHostRuntimeInterfaces;
using Photon.SocketServer.Rpc;

namespace ChatServer
{
    class ChatPeer : ClientPeer    //新版本这个继承ClientPeer
    {
        public ChatPeer(InitRequest initRequest) : base(initRequest)
        {

        }

        //当客户端发起请求的时候调用
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            Dictionary<byte, object> dict = new Dictionary<byte, object>();
            dict.Add(1, "sucess");
            OperationResponse response = new OperationResponse(1, dict);
            SendOperationResponse(response,sendParameters);         
        }

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {

        }
    }
}

PhotonServer.config配置文件(重点)
这段代码写在PhotonServer.config的语句块里,如图:
这里写图片描述

 <ChatServer DisplayName="Chat Server">
    <TCPListeners>
      <TCPListener
         IPAddress="0.0.0.0"
         Port="4530"
         OverrideApplication="ChatServer">
        >
      </TCPListener>
    </TCPListeners>

    <Runtime
          Assembly="PhotonHostRuntime, Culture=neutral"
          Type="PhotonHostRuntime.PhotonDomainManager"
          UnhandledExceptionPolicy="Ignore">
    </Runtime>
    <Applications Default="ChatServer">
    <Application
    Name="ChatServer"
    BaseDirectory="ChatServer"
    Assembly="ChatServer"
    Type="ChatServer.ChatServer"
                ></Application>
          </Applications>
    </ChatServer>

ChatServer

using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatServer
{
  public  class ChatServer : ApplicationBase      //抽象类
    {
        protected override PeerBase CreatePeer(InitRequest initRequest)    //客户端连接我们的服务端时调用这个方法
        {
            return new ChatPeer(initRequest);
        }

        protected override void Setup()
        {

        }

        protected override void TearDown()
        {
            throw new NotImplementedException();
        }
    }
}

配置完config文件后,启动PhotonServer,可以看到新的ChatServer的Application连接,启动成功。
这里写图片描述

再看看是否能接收客户端的请求消息:
这里写图片描述

测试连接成功!

在这里,我给大家展示一下常见的启动错误,例如:

这里写图片描述
这都是因为PhotonServer.config配置文件不对的问题!,按照我的新版本的配置即可解决问题!

下面进行unity的测试:
首先在PhotonServer的lib文件下面导入Photon3Unity3D.dll文件到unity中。
这里写图片描述
这里写图片描述
挂载到任意GameObject上
这里写图片描述

PhotonServerEngine脚本

using UnityEngine;
using System.Collections;
using ExitGames.Client.Photon;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public class PhotonServerEngine : MonoBehaviour,IPhotonPeerListener {

    private PhotonPeer peer;
    private bool isconnect = false;
    void Start()
    {
        peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
        peer.Connect("127.0.0.1:4530", "ChatServer");//连接服务器
    }

    void Update()
    {
        peer.Service();
    }

    void OnGUI()
    {
        if (isconnect)
        {
            if (GUILayout.Button("Send"))
            {
                Dictionary<byte, object> dict = new Dictionary<byte, object>();
                dict.Add(1, "username");
                dict.Add(2, "password");
                peer.OpCustom(1, dict, true);
            }

        }
    }

    public void DebugReturn(DebugLevel level, string message)
    {
        Debug.Log(level + ":" + message);
    }

    public void OnEvent(EventData eventData)
    {

    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        Dictionary<byte, object> dict = operationResponse.Parameters;
        object val = null;
        dict.TryGetValue(1, out val);
        Debug.Log("getserver" + val.ToString());
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
        switch (statusCode)
        {
            case StatusCode.Connect:
                isconnect = true;
                Debug.Log("Connect");
                break;
        }

    }
}

运行,成功连接到服务器。
这里写图片描述

欢迎你关注我的博客。

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_阿松先生

感谢您的支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值