Photon Server server-side programming

Photon Server and Unity3D data exchange:

Photon Server server-side programming

Unity3D client programming

One: Download and install the Photon Server:

https://www.photonengine.com/zh-CN/sdks#server-sdkserverserver

Download Download SDK (registration required landing download)

Two: Photon Server Client Programming:

 

1, the new project MyGameServer, reference external libraries (5) and set PhotonServer.config file.

 

Set PhotonServer.config file

 

 

 1 <MMoInstance  <!--这个Photon instances的名称-->
 2         MaxMessageSize="512000"
 3         MaxQueuedDataPerPeer="512000"
 4         PerPeerMaxReliableDataInTransit="51200"
 5         PerPeerTransmitRateLimitKBSec="256"
 6         PerPeerTransmitRatePeriodMilliseconds="200"
 7         MinimumTimeout="5000"
 8         MaximumTimeout="30000"
 9         DisplayName="MyGame"  <!--显示在Photon instances的名称-->
10         >
11         
12         <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
13         <!-- Port 5055 is Photon's default for UDP connections. -->
14         <UDPListeners>
15             <UDPListener
16                 IPAddress="0.0.0.0"
17                 Port="5055"
18                 OverrideApplication="MyGame1">"<!--指明这个端口号是给哪个Application使用的-->
19             </UDPListener>
20         </UDPListeners>
21     
22         <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
23         <!-- Port 4530 is Photon's default for TCP connecttions. -->
24         <!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> 
25         <TCPListeners>
26             <TCPListener
27                 IPAddress="0.0.0.0"
28                 Port="4530"
29                 PolicyFile="Policy\assets\socket-policy.xml"
30                 InactivityTimeout="10000"
31                 OverrideApplication="MyGame1"                
32                 >
33             </TCPListener>
34         </TCPListeners>
35 
36         <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
37         <PolicyFileListeners>
38           <!-- multiple Listeners allowed for different ports -->
39           <PolicyFileListener
40             IPAddress="0.0.0.0"
41             Port="843"
42             PolicyFile="Policy\assets\socket-policy.xml"
43             InactivityTimeout="10000">
44           </PolicyFileListener>
45           <PolicyFileListener
46             IPAddress="0.0.0.0"
47             Port="943"
48             PolicyFile="Policy\assets\socket-policy-silverlight.xml"
49             InactivityTimeout="10000">
50           </PolicyFileListener>
51         </PolicyFileListeners>
52 
53         <!-- WebSocket (and Flash-Fallback) compatible listener -->
54         <WebSocketListeners>
55             <WebSocketListener
56                 IPAddress="0.0.0.0"
57                 Port="9090"
58                 DisableNagle="true"
59                 InactivityTimeout="10000" 60 OverrideApplication="MyGame1">
61             </WebSocketListener> 62 </WebSocketListeners> 63  64 <!-- Defines the Photon Runtime Assembly to use. --> 65 <Runtime 66 Assembly="PhotonHostRuntime, Culture=neutral" 67 Type="PhotonHostRuntime.PhotonDomainManager" 68 UnhandledExceptionPolicy="Ignore"> 69 </Runtime> 70 71 72 <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. --> 73 <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. --> 74 <Applications Default="MyGame1"><!- connecting client connection server is not specified the default Application Application -> 75 76 <-! MMO Demo Application -> 77 <Application 78 Name = "MyGame1" <-! Application name -> 79 BaseDirectory = "MyGameServer" <-! \ Deploy the file name of the server application -> 80 Assembly = "MyGameServer" <-! Assembly name -> 81 type = "MyGameServer.MyGames" <- main class name -> 82 ForceAutoRestart = "true" <- restart automatically -> 83 WatchFiles =!! "dll; config" 84 ExcludeFiles = " log4net.config "> 85 </ Application> 86 87 </ Applications> 88 </ MMoInstance>

 

2, the new class inherits MyGames ApplicationBase as a server class starts, and abstract method implementation.

The using. 1 the System.Linq; 
 2 the using the System.Text; 
 . 3 the using System.Threading.Tasks; 
 . 4 the using ExitGames.Logging;  . 5 the using Photon.SocketServer;  . 6 the using log4net.config;. 7 the using ExitGames.Logging.Log4Net; namespace. 8. 9 MyGameServer 10 {public class. 11 MyGames: 12 is the ApplicationBase {/// 13 is <Summary> 14 /// get log namespace object reference ExitGames.Logging /// 15 </ Summary> public static Readonly ILogger the log 16 = LogManager.GetCurrentClassLogger () ; /// 1718 Executive <summary> 19 /// client connection request 20 /// </ summary> 21 /// <param name = "initRequest"> client information </ param> 22 /// <returns> </ returns> 23 protected overrideCreatePeer PeerBase (initRequest initRequest) 24 {25 log.info ( "client connection success .....!" ); Return 26 is new new ClientPeers (initRequest); 27 } 28 29 /// <Summary> 30 /// Initialization /// 31 is </ Summary> 32 protected the override void the Setup () 33 is {34 is log4net.GlobalContext.Properties [ "the Photon: ApplicationLogPath"] = Path.Combine (Path.Combine (this.ApplicationRootPath, "bin_Win64"), "log " ); // 35 System.IO namespace references the FileInfo ConfigInfo = log new new setting the FileInfo 36 (Path.Combine (this.BinaryPath," log4net.config " )); 37 [IF (configInfo.Exists) 38 is {39 // references ExitGames.Logging.Log4Net namespace 40 LogManager.SetLoggerFactory (Log4NetLoggerFactory.Instance); // 41 // widget set use log4net namespace references log4net.Config 42 XmlConfigurator.ConfigureAndWatch (configInfo); // read log files 43} 44 Log.Info ( "initialized successfully .....!" ); 45 } 46 is /// <Summary> 47 /// /// closed 48 </ Summary> 49 protected the override void the TearDown () 50 { 51 log.Info ( "server successfully closed .....!" ); 52 } 53 } 54}

 

3, a client connection class inherits ClientPeer ClientPeers class and its abstract methods.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Photon.SocketServer; 7 using PhotonHostRuntimeInterfaces; 8 9 namespace MyGameServer 10 { 11 public class ClientPeers :ClientPeer 12  { 13 public ClientPeers(InitRequest initRequest):base(initRequest) 14  { 15  } 16 17 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) 18  { 19 MyGames.Log.Info("客户端断开连接!....."); 20  } 21 22 protected override voidOnOperationRequest (OperationRequest operationRequest, SendParameters sendParameters) 23 is {24 // client requests 25 switch according to the type classification (operationRequest.OperationCode) 26 is {Case. 1 27 : 28 // Object client data obtained 29 I, J; the Dictionary 30 <byte, Object> = DATE operationRequest.Parameters; 31 is date.TryGetValue (. 1, OUT I); 32 date.TryGetValue (2, OUT J); // log output 33 is 34 MyGames.Log.Info (String.Format ( "receive a request {0} .....,. 1 {} "! , I, J)); 35 // return information about the client OperationResponse OP = new new OperationResponse 36 (. 1 ); 37 [op.Parameters = DATE; // SendOperationResponse 38 is only when the two-way interaction (ie by the client makes a request, then there returned from the server), by the service to client. 39 SendOperationResponse (OP, sendParameters); 40 // 41 is the EventData eventDataA unilaterally sending a message to the client by the server = new EventData (1); 42 eventData.Parameters = date; 43  SendEvent(eventData, sendParameters); 44 break; 45 case 2: 46 break; 47 default: 48 break; 49  } 50  } 51  } 52 }

 4, the introduction of logging configuration file log4net.config

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <log4net debug="false" update="Overwrite">
 3 
 4   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
 5     <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" />
 6     <!--MyGame.Server修改为自己想要的日志文件名称-->
 7     <appendToFile value="true" />
 8     <maximumFileSize value="5000KB" />
 9     <maxSizeRollBackups value="2" />
10     <layout type="log4net.Layout.PatternLayout">
11       <conversionPattern value="%d [%t] %-5p %c - %m%n" />
12     </layout>
13   </appender>
14 
15   <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
16     <layout type="log4net.Layout.PatternLayout">
17       <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
18     </layout>
19     <filter type="log4net.Filter.LevelRangeFilter">
20       <levelMin value="DEBUG" />
21       <levelMax value="FATAL" />
22     </filter>
23   </appender>
24 
25   <!-- logger -->
26   <root>
27     <level value="INFO" />
28     <!--<appender-ref ref="ConsoleAppender" />-->
29     <appender-ref ref="RollingFileAppender" />
30   </root>
31 
32   <logger name="OperationData">
33     <level value="INFO" />
34   </logger>
35 
36 </log4net>

 

Guess you like

Origin www.cnblogs.com/unknown6248/p/11462702.html