搭建服务器端

Posted fzxiaoyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建服务器端相关的知识,希望对你有一定的参考价值。

 1.使用VS新建个项目ChatServer

 2.给项目添加所需的lib引用(C:\\Program Files\\Photon Server\\lib\\)

PhotonHostRuntimeInterfaces.dll  主机运行时接口dll

ExitGames.Logging.Log4Net.dll    退出游戏时日志信息dll

log4net.dll  日志信息dll

Photon.SocketServer.dll  服务器套接字dll

ExitGamesLibs.dll  退出游戏相关libdll

 

 3.创建ChatServer入口类继承 ApplicationBase并实现接口

 ChatServer

using Photon.SocketServer;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ChatServer

{

    //继承自ApplicationBase的类是server的入口程序

    class ChatServer : ApplicationBase

    {

        //当有新的客户连接到这个server时被自动调用

        protected override PeerBase CreatePeer(InitRequest initRequest)

        {

            //每次被调用就创建一个继承自PeerBase类的对象用于跟客户端通信

            return new ChatPeer(initRequest.Protocol, initRequest.PhotonPeer);

        }

        //server启动时被调用

        protected override void Setup()

        {

 

        }

        //server停止时被调用

        protected override void TearDown()

        {

            

        }

    }

}

 4.新建ChatPeer类继承ChatBase,并实现接口

 ChatPeer 

using Photon.SocketServer;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using PhotonHostRuntimeInterfaces;

 

namespace ChatServer

{

    //与客户端通信的类

    class ChatPeer : PeerBase

    {

        public ChatPeer(IRpcProtocol ircp,IPhotonPeer peer) : base(ircp,peer)

        {

 

        }

        /// <summary>

        /// 当客户端断开连接时被调用

        /// </summary>

        /// <param name="reasonCode">断开连接原因</param>

        /// <param name="reasonDetail">具体描述文本</param>

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)

        {

            

        }

        /// <summary>

        /// 当客户端发起请求时被调用

        /// </summary>

        /// <param name="operationRequest">客户端的操作请求</param>

        /// <param name="sendParameters">发送的参数</param>

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)

        {

            OperationResponse opRequerst = new OperationResponse();

            Dictionary<byte, object> dict = new Dictionary<byte, object>();

            dict.Add(1, "cctv");

            dict.Add(2, "123456");

            opRequerst.Parameters = dict;

            //对客户端的操作请求进行回复

            SendOperationResponse(opRequerst, sendParameters);

        }

    }

}

 5.设置项目输出路径

 首先找到photon安装目录(C:\\Program Files\\Photon Server\\deploy\\)

deploy下新建个文件夹ChatServer, ChatServer下再新建个文件夹叫bin

最后在VS项目的属性面板选择输出路径为

C:\\Program Files\\Photon Server\\deploy\\ChatServer\\bin\\

最后生成解决方案.

6.打开PhotonServer.config文件,(C:\\Program Files\\Photon Server\\deploy\\bin_Win64)

 

 复制下面的代码粘贴到</Configuration>上面一个空行, 相当于添加了一个ChatServer标签

 折叠的代码如下:

  <ChatServer
  MaxMessageSize="512000"
  MaxQueuedDataPerPeer="512000"
  PerPeerMaxReliableDataInTransit="51200"
  PerPeerTransmitRateLimitKBSec="256"
  PerPeerTransmitRatePeriodMilliseconds="200"
  MinimumTimeout="5000"
  MaximumTimeout="30000">

    <!-- 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">
      </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"
                >
      </TCPListener>
    </TCPListeners>

    <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
    <PolicyFileListeners>
      <!-- multiple Listeners allowed for different ports -->
      <PolicyFileListener
            IPAddress="0.0.0.0"
            Port="843"
            PolicyFile="Policy\\assets\\socket-policy.xml"
            InactivityTimeout="10000">
      </PolicyFileListener>
      <PolicyFileListener
            IPAddress="0.0.0.0"
            Port="943"
            PolicyFile="Policy\\assets\\socket-policy-silverlight.xml"
            InactivityTimeout="10000">
      </PolicyFileListener>
    </PolicyFileListeners>

    <!-- WebSocket (and Flash-Fallback) compatible listener -->
    <WebSocketListeners>
      <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9090"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="ChatServer">
      </WebSocketListener>
    </WebSocketListeners>

    <!-- 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="ChatServer">
      
      <!-- ChatServer Application -->
      <Application
              Name="ChatServer"
              BaseDirectory="ChatServer"      
              Assembly="ChatServer"
              Type="ChatServer.ChatServer"
              ForceAutoRestart="true"
              WatchFiles="dll;config"
              ExcludeFiles="log4net.config">
            </Application>

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

    </Applications>
  </ChatServer>
ChatServer

7.最后运行PhotonControl.exe,会出现一个我们新添加的标签ChatServer,选择ChatServer->start as appliaction, 如果正常显示蓝色图标,

出现异常显示灰色图标.

以上是关于搭建服务器端的主要内容,如果未能解决你的问题,请参考以下文章

Qt低功耗蓝牙系列四(搭建低功耗服务端代码)

Qt低功耗蓝牙系列四(搭建低功耗服务端代码)

Qt低功耗蓝牙系列四(搭建低功耗服务端代码)

golang代码片段(摘抄)

Git 服务器搭建

node 环境下简单web服务器搭建代码