Photon_部署PhotonServer 服务端项目_011

来源:互联网 发布:java模拟发送get请求 编辑:程序博客网 时间:2024/06/03 19:52

第一步创建项目

首先创建一个VS项目,不同的是这次创建的项目是类库
这里写图片描述


第二步创建部署的目录

在deploy目录下面创建MyGameServer目录,并在MyGameServer下创建bin目录
这里写图片描述


第三步修改项目的生成目录

因为此项目是一个类库项目,所以会生成一些文件
我们将这些文件直接放入第二步创建的目录中
这里写图片描述
这里写图片描述
这里写图片描述
这样在我们完成项目的时候选择项目,右键,点击生成就可以在此目录下面看到生成的文件了


第四步 添加必要的引用

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


第五步创建主类

创建MyGameServer类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Photon.SocketServer;using ExitGames.Logging;using ExitGames.Logging.Log4Net;using System.IO;using log4net.Config;namespace MyGameServer{    //所有的server端 主类都要继承自ApplicationBase类    //ApplicationBase 是一个抽象类 我们需要实现他的所有的抽象方法    public class MyGameServer : ApplicationBase    {        //当一个客户端请求连接的时候        //我们使用peerbase 表示和一个客户端的连接        protected override PeerBase CreatePeer(InitRequest initRequest)        {            return new ClientPeer(initRequest);        }        //初始化        protected override void Setup()        {        }        //Server端关闭的时候        protected override void TearDown()        {        }    }}

第六步 创建一个用户连接类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Photon;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)        {            throw new NotImplementedException();        }        //发送请求        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)        {            throw new NotImplementedException();        }    }}

第七步配置文件

该配置文件位于\deploy\bin_Win64文件夹下
这里写图片描述
此文件是一个文本文件,为了便于我们编辑我们可以使用VS打开
在此文件中Configurtion标签中包含两个系统自带的项目
LoadBalancing和MMoDemo
类似下面的图
这里写图片描述

我们可以复制其中一个开始更改我们现在要部署的项目

    <!-- Instance settings -->  <!-- DisplayName 就是项目启动时启动的名字 -->    <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 -->      <!--BaseDirectory="MyGameServer" 我们在deploy下创建的目录-->      <!--Assembly="MyGameServer" 程序集-->      <!--Type="MyGameServer.MyGameServer" 就是主类的路径 含义:在MyGameServer命名空间下的MyGameServer类-->            <Application                Name="MyGame1"                BaseDirectory="MyGameServer"                Assembly="MyGameServer"                Type="MyGameServer.MyGameServer"                ForceAutoRestart="true"                WatchFiles="dll;config"                ExcludeFiles="log4net.config">            </Application>        </Applications>    </MyGameInstance>

这样我们就可以启动自己的项目了