PhotonServer介绍

官网 On-Premises Cross Platform Multiplayer Game Backend | Photon Engine

Photon为您完全封装了每个客户端平台的复杂网络层。Photon Server支持可靠的UDP,TCP,HTTP和Web套接字,每种都具有精简和纤薄的传输协议。
您的游戏客户端跨平台和跨协议进行通信:只需发送数据,Photon进行反序列化/序列化。

光子服务器架构

Photon Core采用原生C ++编写,可提供最佳性能并支持一流的工具链。结合Windows Server IO完成端口(IOCP),它实现了协议的大量“提升”。
业务逻辑在.NET CLR中执行,包含用C#或任何其他.NET语言编写的应用程序。

所有应用程序都位于可扩展的RPC和事件开发框架之上,以解决常见任务。
RPC调用可以简单灵活地映射到操作实例。基于使用光纤的消息传递原理简化了线程。总的来说,Photon Server旨在在CPU成为瓶颈之前使流量饱和。

下载PhotonServer SDK

解压到非中文目录

 文件夹介绍

build:

deploy:部署的服务端程序

doc:文档

lib:动态链接库  重要的有Photon3Unity3D.dll

src-server:官方提供的demo

如何启动?

  根据操作系统,32位在bin win32文件夹,64位在bin win64文件夹中 运行PhotonControl.exe

 

免费用户只能同时链接20个用户,注册后可连接100个。在官网免费注册,下载license文件,放到PhotonControl.exe所在文件夹。

配置文件  PhotonServer.config  配置要部署的程序

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

<?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>

             

        </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>

             

            <!-- 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>

             

        </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="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>

  <!-- Instance settings -->

  <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>   UDP

      <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>   TCP

      <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 -->

      <Application

                Name="MyGame1"  应用名

                BaseDirectory="MyGameServer"   存放的文件夹

                Assembly="MyGameServer"         程序集名称

                Type="MyGameServer.MyGameServer"    启动名称

                ForceAutoRestart="true"    强制重启

                WatchFiles="dll;config"

                ExcludeFiles="log4net.config">   日志文件

      </Application>

    </Applications>

  </MyGameInstance>

</Configuration>

  

创建第一个服务器端项目,并部署

创建类库项目

 配置要求

将生成路径改为deploy/MyGameServer/bin,该文件夹位自己创建

右键项目重新生成后,查看该文件夹,发现生成MyGameServer.dll即成功

开发ApplicationBase和ClientPeer

  添加引用  

添加主类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

/// <summary>

    /// 主类,启动类,类似Main()

    /// 所有主类都继承自ApplicationBase

    /// </summary>

    class MyGameServer : ApplicationBase

    {

        /// <summary>

        /// 客户端请求链接

        /// </summary>

        /// <param name="initRequest"></param>

        /// <returns></returns>

        protected override PeerBase CreatePeer(InitRequest initRequest)

        {

           return new ClientPeer(initRequest);

        }

        /// <summary>

        ///server启动时 初始化

        /// </summary>

        protected override void Setup()

        {

            throw new NotImplementedException();

        }

        /// <summary>

        /// server关闭

        /// </summary>

        protected override void TearDown()

        {

            throw new NotImplementedException();

        }

    }

  ClientPeer类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

/// <summary>

    /// 管理跟客户端的链接

    /// </summary>

    public class ClientPeer : Photon.SocketServer.ClientPeer

    {

        public ClientPeer(InitRequest initRequest) : base(initRequest)

        {

        }

        /// <summary>

        /// 断开链接

        /// </summary>

        /// <param name="reasonCode"></param>

        /// <param name="reasonDetail"></param>

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)

        {

            throw new System.NotImplementedException();

        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)

        {

            throw new System.NotImplementedException();

        }

    }

  更改配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<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 -->

      <Application

                Name="MyGame1"

                BaseDirectory="MyGameServer"

                Assembly="MyGameServer"

                Type="MyGameServer.MyGameServer"

                ForceAutoRestart="true"

                WatchFiles="dll;config"

                ExcludeFiles="log4net.config">

      </Application>

    </Applications>

  </MyGameInstance>

  

日志文件Log

log4net

从src-server\Mmo\Photon.MmoDemo.Server复制log4net.config到项目根目录  属性设为始终复制

添加两个引用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<?xml version="1.0" encoding="utf-8" ?>

<log4net debug="false" update="Overwrite">

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">

    <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyServer.Server.log" />

    <appendToFile value="true" />

    <maximumFileSize value="5000KB" />

    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="%d [%t] %-5p %c - %m%n" />

    </layout>

  </appender>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">

    <layout type="log4net.Layout.PatternLayout">

      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />

    </layout>

    <filter type="log4net.Filter.LevelRangeFilter">

      <levelMin value="DEBUG" />

      <levelMax value="FATAL" />

    </filter>

  </appender>

  <!-- logger -->

  <root>

    <level value="INFO" />

    <!--<appender-ref ref="ConsoleAppender" />-->

    <appender-ref ref="RollingFileAppender" />

  </root>

  <logger name="OperationData">

    <level value="INFO" />

  </logger>

</log4net>

  更改MyGameServer类  配置日志

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

using System;

using System.IO;

using ExitGames.Logging;

using ExitGames.Logging.Log4Net;

using log4net.Config;

using Photon.SocketServer;

namespace MyGameServer

{

    /// <summary>

    /// 主类,启动类,类似Main()

    /// 所有主类都继承自ApplicationBase

    /// </summary>

    class MyGameServer : ApplicationBase

    {

        private static readonly ILogger log = LogManager.GetCurrentClassLogger();

        /// <summary>

        /// 客户端请求链接

        /// </summary>

        /// <param name="initRequest"></param>

        /// <returns></returns>

        protected override PeerBase CreatePeer(InitRequest initRequest)

        {

            log.Info("一个客户端链接过来了");

           return new ClientPeer(initRequest);

        }

        /// <summary>

        ///server启动时 初始化

        /// </summary>

        protected override void Setup()

        {

            //日志初始化

            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =

                Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");

                 

            FileInfo configFileInfo=new FileInfo(Path.Combine(this.BinaryPath,"log4net.config"));

            if (configFileInfo.Exists)

            {

                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);

                XmlConfigurator.ConfigureAndWatch(configFileInfo);

            }

            log.Info("Setup Completed");

        }

        /// <summary>

        /// server关闭

        /// </summary>

        protected override void TearDown()

        {

            log.Info("服务端关闭");

           // throw new NotImplementedException();

        }

    }

}

使用了PhotonServer之后本公司业务量剧增用户数量呈指数型增加非常好用!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Photon Server 是一种用于实时多人游戏和应用程序的实时通信解决方案,旨在提供稳定、可靠且高效的通讯网络。它采用了客户端-服务器架构,由客户端和服务器之间的交互来实现实时通信。 Photon Server 通过使用 Photon Realtime SDK 提供了跨平台的客户端库,使开发人员能够在各种设备和操作系统上创建多人游戏和应用程序。它支持多种编程语言,如C++、C#和JavaScript,使开发人员能够根据自己的喜好和经验选择合适的编程语言Photon Server 使用可扩展的云基础设施来处理大量的并发连接和数据传输。它具有灵活的服务器配置,使开发人员能够按照自己的需求来分配资源和管理服务器。同时,它提供了令牌验证和防止作弊的机制,确保通信的安全性和公平性。 在使用 Photon Server 进行通讯时,开发人员可以利用其提供的API来处理客户端和服务器之间的消息交换。这些消息可以是玩家输入、游戏事件或其他自定义信息。开发人员可以定义自己的逻辑和规则,并使用 Photon Server 提供的功能来同步游戏状态、处理用户之间的交互以及实现多人游戏的功能。 总而言之,Photon Server 是一个功能强大的实时通信解决方案,为开发人员提供了一种简单、灵活且可靠的方法来实现跨平台的多人游戏和应用程序。无论是小型团队开发的独立游戏,还是大型公司开发的在线多人游戏,Photon Server 都能满足各种需求,并提供出色的性能和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值