Photon Server搭建(一)

1.下载环境
环境下载地址

2.解压文件

3.下载Licenses

Licenses下载地址

4.将下载好的Licenses文件复制到deploy\bin_Win64目录

5.成功后如图所示
在这里插入图片描述

6.创建C#类库项目

  1. .NET4.5版本,起名MyGameServer
  2. 引入DLL(lib目录下): ExitGamesLibs.dll、Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll
  3. 代码
    public class MyGameServer : ApplicationBase
    {
        //当一个客户端请求连接的时候
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new ClientPeer(initRequest);
        }
        //初始化
        protected override void Setup()
        {
            
        }
        //server端关闭的时候
        protected override void TearDown()
        {
            
        }
    }
    public class ClientPeer : Photon.SocketServer.ClientPeer
    {
        public ClientPeer(InitRequest initRequest) : base(initRequest)
        {

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

        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {

        }
    }

7.将编译好的dll复制到deploy\MyGameServer\bin,文件夹需要手动创建
8.修改deploy\bin_Win64\PhotonServer.config配置文件

	<MyGameInstance
	MaxMessageSize="512000"
	MaxQueuedDataPerPeer="512000"
	PerPeerMaxReliableDataInTransit="51200"
	PerPeerTransmitRateLimitKBSec="256"
	PerPeerTransmitRatePeriodMilliseconds="200"
	MinimumTimeout="5000"
	MaximumTimeout="30000"
	DisplayName="My Game"
		>

		<UDPListeners>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5055"
				OverrideApplication="MyGame1">
			</UDPListener>
		</UDPListeners>

		<TCPListeners>
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4530"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000"
				OverrideApplication="MyGame1"
				>
			</TCPListener>
		</TCPListeners>

		<Runtime
			Assembly="PhotonHostRuntime, Culture=neutral"
			Type="PhotonHostRuntime.PhotonDomainManager"
			UnhandledExceptionPolicy="Ignore">
		</Runtime>
		<!--BaseDirectory:编译好的dll所在文件夹名-->
		<!--Assembly:dll名-->
		<!--Type:命名空间.类名-->
		<Applications Default="MyGame1">
			<Application
				Name="MyGame1"
				BaseDirectory="MyGameServer"
				Assembly="MyGameServer"
				Type="MyGameServer.MyGameServer"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>

		</Applications>
	</MyGameInstance>

9.添加日志文件
1.引入类库lib\log4net.dll、ExitGames.Logging.Log4Net.dll
2.添加配置文件,复制src-server\Mmo\Photon.MmoDemo.Server\log4net.config到项目中
3.修改配置文件

<file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" />

4.修改log4net.config文件属性为始终复制
5.添加配置日志的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using Photon.SocketServer;

namespace MyGameServer
{
    public class MyGameServer : ApplicationBase
    {
        private static readonly ILogger log = LogManager.GetCurrentClassLogger();
        //当一个客户端请求连接的时候
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new ClientPeer(initRequest);
        }
        //初始化
        protected override void Setup()
        {
            InitLog();
            log.Info("初始化成功");

        }
        //server端关闭的时候
        protected override void TearDown()
        {
            
        }

        #region 日志
        /// <summary>
        /// 初始化日志以及配置
        /// </summary>
        private void InitLog()
        {
            //日志的初始化
            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = this.ApplicationRootPath + @"\bin_Win64\log";//设置日志的路径
            FileInfo configFileInfo = new FileInfo(this.BinaryPath + @"\log4net.config");//获取配置文件
            if (configFileInfo.Exists)
            {
                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
                XmlConfigurator.ConfigureAndWatch(configFileInfo);
            }
        }
        #endregion
    }
}

6.添加日志的文件到日志控制台上

7.服务器和unity端交互,基本代码
服务器端

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

namespace MyGameServer
{
    public class ClientPeer : Photon.SocketServer.ClientPeer
    {
        public ClientPeer(InitRequest initRequest) : base(initRequest)
        {

        }
        //处理客户端断开的后续工作
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {

        }
        //处理客户端的请求
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch (operationRequest.OperationCode)
            {
                case 1:
                    //收到
                    Dictionary<byte, object> data = operationRequest.Parameters;
                    MyGameServer.log.Info("收到客户端消息:" + data[1].ToString());

                    //返回
                    OperationResponse operationResponse = new OperationResponse();
                    operationResponse.OperationCode = 1;
                    Dictionary<byte, object> data2 = new Dictionary<byte, object>();
                    data2.Add(1, "你好,我是服务器");
                    operationResponse.Parameters = data2;
                    SendOperationResponse(operationResponse, sendParameters);
                    break;
                default:
                    break;
            }
        }
    }
}

unity端

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

public class PhotonEngine : MonoBehaviour, IPhotonPeerListener
{
    private PhotonPeer peer;
    void Start()
    {
        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055", "MyGame");
    }

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

    public void DebugReturn(DebugLevel level, string message)
    {

    }
    /// <summary>
    /// 接收服务器事件
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEvent(EventData eventData)
    {

    }
    /// <summary>
    /// 接收服务器响应
    /// </summary>
    /// <param name="operationResponse"></param>
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        
    }
    /// <summary>
    /// 状态改变
    /// </summary>
    /// <param name="statusCode"></param>
    public void OnStatusChanged(StatusCode statusCode)
    {
        Debug.Log(statusCode);
    }

    /// <summary>
    /// 向服务器发送响应
    /// </summary>
    public void SendOperation()
    {
        peer.OpCustom(1, new Dictionary<byte, object>(), true);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呼呼突突

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值