PhotonServer的安装与简单配置使用

本文介绍PhotonServer的安装步骤及简单配置方法,并演示如何通过MyGameServer实例输出日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PhotonServer 是一款实时Socket服务器,开发起来简单易用,被很多游戏公司采用。

今天来讲一下Photon的安装与简单配置以及如何输出日志。

1.首先到Photon官网,注册账号,下载ServerSDK。是一个压缩文件,下载完毕解压到对应文件夹,路径不要包含中文。

解压之后 如上图所示。启动程序在deploy文件夹中,bin_win64 中PhotonControl.exe。启动之后在右下角会有标志。

之后我们需要把我们的应用部署到这里,才能使用,我们在bin_win64中新建我们的项目文件夹MyGameServer,在文件夹

中新建bin 文件夹,名字一定是bin 默认的。用于存放我们的项目生成的dll文件。

打开VS 新建解决方案,新建类库MyGameServer,生成目录改成上面我们添加的bin文件夹。

在编写程序的时候要添加几个应用,应用文件在deploy文件夹下面的bin_win64下,如下图

2.创建我们的主类MyGameServer,继承与ApplicationBase。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using System.IO;
using log4net.Config;
namespace MyGameServer
{
    //所有Server端的主类 都要继承 ApplicationBase,实现类中的方法
    public class MyGameServer : ApplicationBase
    {
        private static readonly ILogger log = LogManager.GetCurrentClassLogger();
        //当客户端链接过来,这里要创建一个peer,处理连接请求
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new ClientPeer(initRequest);
        }

        //初始化
        protected override void Setup()
        {
            //日志初始化

            //配置 日志输出 文件 保存文件夹(和photon系统其他日志文件放到一起)
            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(
                Path.Combine(this.ApplicationRootPath, "bin_win64"), "log");
            //读取目录下的文件
            FileInfo configInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
            if (configInfo.Exists)
            {
                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
                //让log4net  这个插件 读取这个配置文件
                XmlConfigurator.ConfigureAndWatch(configInfo);
            }
             //测试日志输出
            log.Info("InstSuccess");

        }

 

        //当服务端关闭的时候
        protected override void TearDown()
        {
            
        }
    }

}

3.创建处理客户端 的类

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

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)
        {
           
        }
    }
}

4.配置PhotonServerConfig文件

可以直接把MMODemo 配置文件复制下来,做更改。

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

<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
<!-- Port 5055 is Photon's default for UDP connections. -->
<UDPListeners>
<UDPListener
IPAddress="0.0.0.0"
Port="5055"
OverrideApplication="MyGame1">
</UDPListener>
</UDPListeners>
    
<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
<!-- Port 4530 is Photon's default for TCP connecttions. -->
<!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> 
<TCPListeners>
<TCPListener
IPAddress="0.0.0.0"
Port="4530"
PolicyFile="Policy\assets\socket-policy.xml"
InactivityTimeout="10000"
OverrideApplication="MyGame1"
>
</TCPListener>
</TCPListeners>

<!-- Defines the Photon Runtime Assembly to use. -->
<Runtime
Assembly="PhotonHostRuntime, Culture=neutral"
Type="PhotonHostRuntime.PhotonDomainManager"
UnhandledExceptionPolicy="Ignore">
</Runtime>

<!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->
<!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->
<Applications Default="MyGame1">

<!-- CounterPublisher Application -->
<Application
Name="MyGame1"
BaseDirectory="MyGameServer"
Assembly="MyGameServer"
Type="MyGameServer.MyGameServer"
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</MyGameInstance>

5.输出日志文件log4net的配置

只需更改其中一句

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

本项目改为了MyGame.Server.log,日志文件名

此时Photon的初始化工作已经完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值