Photon Server伺服務器在LoadBalancing的基礎上擴展登陸服務


一,如何創建一個Photon Server服務

  參見此博客 快速了解和使用Photon Server

二, 讓LoadBalancing與自己的服務一起啟動

  原Photonserver.config文件中需要改動的地方有4處

  1.  <UDPListeners> 標簽

    Udp監聽端口

      <UDPListeners>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5055"
				OverrideApplication="Master">
			</UDPListener>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5056"
				OverrideApplication="Game">
			</UDPListener>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5057"
				OverrideApplication="LoginServer">
			</UDPListener>
		</UDPListeners>

  這里配置我自己的服務 LoginServer 監聽端口為5057 所以Unity中鏈接服務器的端口也要改動

    address = "127.0.0.1:5057"; //連接本機ip,端口5055是Photon Lobalancing服務的默認端口

    Server = "LoginServer";
    peer = new PhotonPeer(this, ConnectionProtocol.Udp); //默認使用udp協議
    peer.Connect(address, Server);  

  2.  <TCPListeners> 標簽

    TCP監控端口

    同樣的添加配置    

			<TCPListener
				IPAddress="0.0.0.0"
				Port="4532"
				OverrideApplication="LoginServer"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000">
			</TCPListener>

     3.  <WebSocketListeners>

    WebSocket兼容監聽    

			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9092"
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="LoginServer">
			</WebSocketListener>

   4.  <Applications>標簽

  保留Master Game 等原來的標簽

		<Applications Default="Master">
			<Application
				Name="LoginServer" 
				BaseDirectory="LoginServer"
				Assembly="LoginServer"
				Type="LoginServer.LoginServer"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config"
			>
			</Application>
			.
              .
              .
</Applications>

  完整的Photonserver.config文件  

<?xml version="1.0" encoding="Windows-1252"?>
<!--
    (c) 2015 by Exit Games GmbH, http://www.exitgames.com
    Photon server configuration file.
    For details see the photon-config.pdf.

    This file contains two configurations:
        
        "LoadBalancing"
                Loadbalanced setup for local development: A Master-server and a game-server.
                Starts the apps: Game, Master, CounterPublisher
                Listens: udp-port 5055, tcp-port: 4530, 843 and 943        
    
-->

<Configuration>
    <!-- Multiple instances are supported. Each instance has its own node in the config file. -->
    
    <LoadBalancing
        MaxMessageSize="512000"
        MaxQueuedDataPerPeer="512000"
        PerPeerMaxReliableDataInTransit="51200"
        PerPeerTransmitRateLimitKBSec="256"
        PerPeerTransmitRatePeriodMilliseconds="200"
        MinimumTimeout="5000"
        MaximumTimeout="30000"
        DisplayName="LoadBalancing (MyCloud)">

        <!-- 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="Master">
            </UDPListener>
            <UDPListener
                IPAddress="0.0.0.0"
                Port="5056"
                OverrideApplication="Game">
            </UDPListener>
            <UDPListener
                IPAddress="0.0.0.0"
                Port="5057"
                OverrideApplication="LoginServer">
            </UDPListener>
        </UDPListeners>
        
        <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->        
        <TCPListeners>
            <!-- TCP listener for Game clients on Master application -->
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4530"
                OverrideApplication="Master"
                PolicyFile="Policy\assets\socket-policy.xml"
                InactivityTimeout="10000"
                >
            </TCPListener>
            
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4531"
                OverrideApplication="Game"
                PolicyFile="Policy\assets\socket-policy.xml"
                InactivityTimeout="10000">
            </TCPListener>
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4532"
                OverrideApplication="LoginServer"
                PolicyFile="Policy\assets\socket-policy.xml"
                InactivityTimeout="10000">
            </TCPListener>
            
            <!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4520">
            </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">
          </PolicyFileListener>
          <PolicyFileListener
            IPAddress="0.0.0.0"
            Port="943"
            PolicyFile="Policy\assets\socket-policy-silverlight.xml">
          </PolicyFileListener>
        </PolicyFileListeners>

        
        <!-- WebSocket (and Flash-Fallback) compatible listener -->
        <WebSocketListeners>
            <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9090"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="Master">
            </WebSocketListener>
            
            <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9091"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="Game">
            </WebSocketListener>
            <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9092"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="LoginServer">
            </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="Master">
            <Application
                Name="LoginServer" 
                BaseDirectory="LoginServer"
                Assembly="LoginServer"
                Type="LoginServer.LoginServer"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config"
            >
            </Application>
            <Application
                Name="Master"
                BaseDirectory="LoadBalancing\Master"
                Assembly="Photon.LoadBalancing"
                Type="Photon.LoadBalancing.MasterServer.MasterApplication"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config"
                >
            </Application>
            <Application
                Name="Game"
                BaseDirectory="LoadBalancing\GameServer"
                Assembly="Photon.LoadBalancing"
                Type="Photon.LoadBalancing.GameServer.GameApplication"
                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>
    </LoadBalancing>    
    
    <!-- Instance settings -->
    <MMoDemo
        MaxMessageSize="512000"
        MaxQueuedDataPerPeer="512000"
        PerPeerMaxReliableDataInTransit="51200"
        PerPeerTransmitRateLimitKBSec="256"
        PerPeerTransmitRatePeriodMilliseconds="200"
        MinimumTimeout="5000"
        MaximumTimeout="30000"
        DisplayName="MMO Demo"
        >
        
        <!-- 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="MMoDemo">
            </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="MMoDemo"                
                >
            </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="MMoDemo">
            </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="MMoDemo">
        
            <!-- MMO Demo Application -->
            <Application
                Name="MMoDemo"
                BaseDirectory="MmoDemo"
                Assembly="Photon.MmoDemo.Server"
                Type="Photon.MmoDemo.Server.PhotonApplication"
                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>
    </MMoDemo>
    
</Configuration>
View Code

 

  unity 中使用 PhotonPeer 的時候要注意在銷毀場景的時候關閉鏈接 peer.Disconnect();

 否則unity會在第二次運行的時候出現卡死的狀態.

 PhotonPeer連接服務器的時候會創建一個網絡線程,停止調試並不會自動的將這個線程結束,再次調試的時候Unity就會卡在PhotonPeer創建網絡線程的操作上.......

 

  這是我的登陸服務


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM