Photon Server 和 Unity3D 数据交互:

Photon Server 服务端编程

Unity3D 客户端编程

VS2017 之 MYSQL实体数据模型

一:Photon Server的下载安装:

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

点击下载 Download SDK(需注册登陆下载)

二:Photon Server的服务端编程:

1、新建项目MyGameServer,引用外部库(5个)并设置PhotonServer.config文件。

设置PhotonServer.config文件

 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"><!--客户端连接服务器未指定Application时连接默认的Application-->
75
76 <!-- MMO Demo Application -->
77 <Application
78 Name="MyGame1"<!--应用名称-->
79 BaseDirectory="MyGameServer"<!--\deploy下这个服务器应用的文件名称-->
80 Assembly="MyGameServer"<!-—程序集名称-->
81 Type="MyGameServer.MyGames"<!--主类名称-->
82 ForceAutoRestart="true"<!--是否自动重启-->
83 WatchFiles="dll;config"
84 ExcludeFiles="log4net.config">
85 </Application>
86
87 </Applications>
88 </MMoInstance>

2、新建MyGames类继承ApplicationBase作为服务器启动类,并实现其抽象方法。

 1 using System.Linq;
2 using System.Text;
3 using System.Threading.Tasks;
4 using ExitGames.Logging;
5 using Photon.SocketServer;
6 using log4net.Config;
7 using ExitGames.Logging.Log4Net;
8
9 namespace MyGameServer
10 {
11 public class MyGames : ApplicationBase
12 {
13 /// <summary>
14 /// 获得日志对象 引用ExitGames.Logging命名空间
15 /// </summary>
16 public static readonly ILogger Log = LogManager.GetCurrentClassLogger();
17
18 /// <summary>
19 /// 客户端连接请求时执行
20 /// </summary>
21 /// <param name="initRequest">客户端信息</param>
22 /// <returns></returns>
23 protected override PeerBase CreatePeer(InitRequest initRequest)
24 {
25 Log.Info("客户端连接成功!。。。。。");
26 return new ClientPeers(initRequest);
27 }
28
29 /// <summary>
30 /// 初始化
31 /// </summary>
32 protected override void Setup()
33 {
34 log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"),"log");
35 //引用System.IO命名空间 日志设置
36 FileInfo configInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
37 if (configInfo.Exists)
38 {
39 //引用ExitGames.Logging.Log4Net命名空间
40 LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance); //设置使用log4net插件
41 //引用log4net.Config命名空间
42 XmlConfigurator.ConfigureAndWatch(configInfo);//读取日志文件
43 }
44 Log.Info("初始化成功!。。。。。");
45 }
46 /// <summary>
47 /// 关闭时
48 /// </summary>
49 protected override void TearDown()
50 {
51 Log.Info("服务器成功关闭!。。。。。");
52 }
53 }
54 }

3、客户端连接类ClientPeers继承ClientPeer类并实现其抽象方法。

 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 void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
23 {
24 //根据客户端请求类型分类
25 switch(operationRequest.OperationCode)
26 {
27 case 1:
28 //客户端数据获得
29 object i, j;
30 Dictionary<byte, object> date = operationRequest.Parameters;
31 date.TryGetValue(1,out i);
32 date.TryGetValue(2,out j);
33 //日志输出
34 MyGames.Log.Info(String.Format("收到一个请求!。。。。。{0},{1}",i,j));
35 //返回客户端信息
36 OperationResponse op = new OperationResponse(1);
37 op.Parameters = date;
38 //SendOperationResponse只适用于双向交互时(即已由客户端发出请求,再有服务端返回),由服务端到客户端。
39 SendOperationResponse(op, sendParameters);
40 //单方面由服务端向客户端发送消息
41 EventData eventData = 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、引入日志配置文件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>

下载地址:https://gitee.com/today6/unity

Photon Server 服务端编程的更多相关文章

  1. 《Linux多线程服务端编程》笔记——多线程服务器的适用场合

    如果要在一台多核机器上提供一种服务或执行一个任务,可用的模式有 运行一个单线程的进程 运行一个多线程的进程 运行多个单线程的进程 运行多个多线程的进程 这些模式之间的比较已经是老生常谈,简单地总结 模 ...

  2. QT server服务端如何判断客户端断开连接

    在QT编程中有时会用到server服务端与客户端进行TCP网络通信,服务端部分代码如下: 1.创建server用于监听客户端套接字 this->server = new QTcpServer(t ...

  3. Linux多线程服务端编程一些总结

    能接触这本书是因为上一个项目是用c++开发基于Linux的消息服务器,公司没有使用第三方的网络库,卷起袖子就开撸了.个人因为从业经验较短,主 要负责的是业务方面的编码.本着兴趣自己找了这本书.拿到书就 ...

  4. 《Linux 多线程服务端编程:使用 muduo C++ 网络库》电子版上市

    <Linux 多线程服务端编程:使用 muduo C++ 网络库> 电子版已在京东和亚马逊上市销售. 京东购买地址:http://e.jd.com/30149978.html 亚马逊Kin ...

  5. 《Linux多线程服务端编程:使用muduo C++网络库》上市半年重印两次,总印数达到了9000册

    <Linux多线程服务端编程:使用muduo C++网络库>这本书自今年一月上市以来,半年之内已经重印两次(加上首印,一共是三次印刷),总印数达到了9000册,这在技术书里已经算是相当不错 ...

  6. SVN--下载、安装VisualSVN server 服务端和 TortoiseSVN客户端

    前言: 在http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html的博客中已经很详细地介绍了SVN的服务器--VisualS ...

  7. 《Linux多线程服务端编程》笔记——线程同步精要

    并发编程基本模型 message passing和shared memory. 线程同步的四项原则 尽量最低限度地共享对象,减少需要同步的场合.如果确实需要,优先考虑共享 immutable 对象. ...

  8. 全网最详细的PLSQL Developer + Oracle client的客户端 或者 PLSQL Developer + Oracle server服务端的下载与安装过程(图文详解)

    不多说,直接上干货! 环境说明: 本地没有安装Oracle服务端,oracle服务端64位,是远程连接,因此本地配置PLSQL Developer64位. Oracle database使用在本机部署 ...

  9. ACE服务端编程3:ACE跨平台之分配堆内存

    ACE服务端编程系列的第三篇,探究ACE解决不同编译器之间分配堆内存的差异. 在ACE的官方示例中会看到大量的ACE_NEW_RETURN,ACE_NEW这样的宏,这是ACE为了消除不同编译器编译的代 ...

随机推荐

  1. Codis与RedisCluster的原理详解

    背景介绍 我们先来看一下为什么要做集群,如果我们要部署一个单节点Redis,很明显会遇到单点故障的问题. 首先能想到解决单点故障的方法,就是做主从,但是当有海量存储需求时,单一的主从结构就会出问题,说 ...

  2. 读JDK源码集合部分

    以前读过一遍JDK源码的集合部分,读完了一段时间后忘了,直到有一次面试简历上还写着读过JDK集合部分的源码,但面试官让我说说,感觉记得不是很清楚了,回答的也模模糊糊的,哎,老了记性越来越差了,所以再回 ...

  3. 作为前端的你,CC游戏开发可以上车

    1. 初来乍到 打开 Cocos Creator 点击新建空白项目,在默认布局的左下区域,一个黄黄assets文件夹映入眼帘.作为前端的你对这个文件是不是再熟悉不过了.是的,和你想象的一样,开发游戏中 ...

  4. Unity经典游戏教程之:贪吃蛇

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  5. Unity经典游戏编程之:球球大作战

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  6. LeetCode 85. 冗余连接 II

    题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...

  7. JVM总结(二)

    JVM总结(2)java内存区域.字节码执行引擎 1.内存区域 程序计数器:知道线程执行位置,保证线程切换后能恢复到正确的执行位置. 虚拟机栈:存栈帧.栈帧里存局部变量表.操作栈.动态连接.方法返回地 ...

  8. 两个 github 账号混用,一个帐号提交错误

    问题是这样,之前有一个github帐号,因为注册邮箱的原因,不打算继续使用了,换了一个新的邮箱注册了一个新的邮箱帐号.新账号提交 就会出现下图的问题,但是原来帐号的库还是能正常提交.   方法1:添加 ...

  9. Go中的文件读写

    在 Go 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄 .我们来看一下os包的使用方式. 1.读取文件 os包提供了两种打开文件的方法: Open(name string) ...

  10. Docker 核心技术

    docker是什么?为什么会出现? 容器虚拟化技术:轻量级的虚拟机(但不是虚拟机) 开发:提交代码 ——> 运维:部署 在这中间,因为环境和配置,出现问题 ——> 把代码/配置/系统/数据 ...