引入ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll
1.主类继承ApplicationBase,实现以下三个类
//客户端连接时,处理连接请求
protected override PeerBase CreatePeer(InitRequest initRequest)
//服务器启动时初始化
protected override void Setup()
//server关闭
protected override void TearDown()
2.创建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)
3.unity 创建服务器管理类实现IPhotonPeerListener
//服务器通知(发起的事件)
public void OnEvent(EventData eventData)
//服务器启动时初始化
protected override void Setup()
//server关闭
protected override void TearDown()
日志初始化,需要引入log4net.dll,ExitGames.Logging.Log4Net.dll
//主类 public class ACGCombatGameServer : ApplicationBase { public static readonly ILogger log = LogManager.GetCurrentClassLogger(); //客户端连接时,处理连接请求 protected override PeerBase CreatePeer(InitRequest initRequest) { log.Info("一个客户端连接过来"); return new ClientPeer(initRequest); } //服务器启动时初始化 protected override void Setup() { InitLog(); } //server关闭 protected override void TearDown() { } //初始化日志 private void InitLog() { //设置日志的输出路径 log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"),"log"); //获取日志配置信息 FileInfo logConfig = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")); if(logConfig.Exists) { LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance); //使用log4net插件 XmlConfigurator.ConfigureAndWatch(logConfig); //读取配置文件 } log.Info("日志初始化完成..."); } }