先在phontoserver文件夹里的deploy创建一个文件夹MyGameServer,在它里面创建一个bin文件夹,把生成目录改一下
在创建一个类库,名字为MyGameServer
导入三个引用:ExitGamesLibs.dll Phonto.SocketServer.dll PhontoHostRuntimeInterfaces.dll
创建一个MyGameServer的类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Photon.SocketServer; using ExitGames.Logging; using System.IO; using ExitGames.Logging.Log4Net; using log4net.Config; namespace MyGameServer { //所有的server端 主类都要继承自applicationbase class MyGameServer : ApplicationBase { private static readonly ILogger log = LogManager.GetCurrentClassLogger(); //log文件,用于日志输出 //当一个客户端请求链接的时候 //我们使用peerbase,表示和一个客户端的连接 protected override PeerBase CreatePeer(InitRequest initRequest) { log.Info("一个客户端链接过来了..."); return new ClientPeer(initRequest); } //初始化 protected override void Setup() { //日志的初始化 log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =Path.Combine( Path.Combine(this.ApplicationRootPath,"bin_Win64"), "log"); //日志输出的目录 FileInfo configFileInfo = new FileInfo(Path.Combine( this.BinaryPath,"log4net.config")); if(configFileInfo.Exists) { LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance); //让photon知道我们使用哪个插件 XmlConfigurator.ConfigureAndWatch(configFileInfo); //让log4net这个插件读取配置文件 } log.Info("setup completed!"); } //server端关闭的时候 protected override void TearDown() { log.Info("服务器端关闭"); } } }
创建一个ClientPeer类
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) { } } }
在PhotonServer.config里创建下面代码<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"> <!-- MMO Demo Application --> <Application Name="MyGame1" BaseDirectory="MyGameServer" Assembly="MyGameServer" Type="MyGameServer.MyGameServer" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application> </Applications> </MyGameInstance> </Configuration>
添加log4net插件,添加log4net.dll ExitGames.Logging.Log4Net.dll引用
复制粘贴log4net.config文件(在src-server,Mmo,Photon.MmoDemo.Server文件下) 始终复制
<?xml version="1.0" encoding="utf-8" ?> <log4net debug="false" update="Overwrite"> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" /> <appendToFile value="true" /> <maximumFileSize value="5000KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="FATAL" /> </filter> </appender> <!-- logger --> <root> <level value="INFO" /> <!--<appender-ref ref="ConsoleAppender" />--> <appender-ref ref="RollingFileAppender" /> </root> <logger name="OperationData"> <level value="INFO" /> </logger> </log4net>