Photon Server 实现注册与登录(二) --- 服务端代码整理
一、有的代码前端和后端都会用到。比如一些请求的Code。使用需要新建项目存放公共代码。
新建项目Common存放公共代码:
EventCode :存放服务端自动发送信息给客户端的code
OperationCode : 区分请求和响应
ReturnCode : 服务端响应码
ParameterCode : 解析参数
Toos/DicTool.cs : 数据基本上都是用Dictionary读取,这里工具话一下。

二、代码
Toos/DicTool.cs
using System.Collections.Generic; namespace Common.Toos
{
public class DictTool
{
//定义一个静态函数,传入key,就能取出Dict中相应的值
public static T2 GetValue<T1, T2>(Dictionary<T1, T2> dict, T1 key)
{
T2 value;
bool ok = dict.TryGetValue(key, out value);
if (ok)
{
return value;
}
else
{
return default(T2);
}
}
}
}
EventCode.cs
namespace Common
{
//服务端自动向客户端发送事件类型
public enum EventCode : byte
{ }
}
OperationCode.cs
namespace Common
{
//区分请求和响应的请求
public enum OperationCode : byte
{
Login,
Register,
Default
}
}
ReturnCode.cs
namespace Common
{
public enum ReturnCode
{
Success,
Failed
}
}
ParameterCode.cs
namespace Common
{
//区分传输数据时的参数类型
public enum ParameterCode:byte
{
UserName,
Password
}
}
三、实现注册和登录,两个请求。服务端接收前端请求主要是在ClientPeer.cs文件的 OnOperationRequest() 函数中。如果今后请求变多,那这一块代码就会越来越多。所以现在整理一下代码,使用模块化区分开每个请求。
使用Handler区分每个请求,让各自的请求处理自己的逻辑。基础Handler定义 OnOperationRequest() 函数,其他Handler都继承这个BaseHandler.
导入公共类Common:

新建目录和文件。BaseHandler 父类,LoginHandler和RegisterHandler分别处理登录和注册,DefaultHandler处理默认请求。

(1)、BaseHandler.cs,其他所有handler类都继承该类。
using Common;
using Photon.SocketServer; namespace MyGameServer.Hander
{
public abstract class BaseHandler
{
//区分请求接口
public OperationCode OpCode; public abstract void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer);
}
}
(2)、DefaultHandler.cs
using Common; using Photon.SocketServer; namespace MyGameServer.Hander
{
public class DefaultHandler:BaseHandler
{
public DefaultHandler()
{
OpCode = OperationCode.Default;
} public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
{ }
}
}
(3)、LoginHandler.cs
using System;
using Common;
using Common.Toos;
using MyGameServer.Manager;
using Photon.SocketServer; namespace MyGameServer.Hander
{
public class LoginHander:BaseHandler
{
public LoginHander()
{
OpCode = OperationCode.Login;
} public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
{ }
}
}
(4)、RegisterHandler.cs
using Common;
using Common.Toos;
using MyGameServer.Manager;
using MyGameServer.Model;
using Photon.SocketServer; namespace MyGameServer.Hander
{
public class RegisterHandler:BaseHandler
{
public RegisterHandler()
{
OpCode = OperationCode.Register;
} public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
{ }
}
}
四、注册Handler。添加Handler后,我们需要使用方便,得在主函数中进行注册。
修改MyGameSerer.cs文件:
(1)、添加一个变量,用于存放Handler
public Dictionary<OperationCode,BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>();
(2)、添加MyGameServer管理单例类。
public new static MyGameServer Instance { get; private set; }
(3)、注册Handler,在Setup()函数中添加
Instance = this;
InitHandler();
(4)、添加Handler,新建函数 InitHandler()
private void InitHandler()
{
LoginHander loginHander = new LoginHander();
HandlerDict.Add(loginHander.OpCode,loginHander); DefaultHandler defaultHandler = new DefaultHandler();
HandlerDict.Add(defaultHandler.OpCode,defaultHandler); RegisterHandler registerHandler = new RegisterHandler();
HandlerDict.Add(registerHandler.OpCode,registerHandler);
}
(5)、完整代码
using System.Collections.Generic;
using System.IO;
using Common;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net;
using log4net.Config;
using MyGameServer.Hander;
using MyGameServer.Manager;
using MyGameServer.Model;
using Photon.SocketServer;
using LogManager = ExitGames.Logging.LogManager; namespace MyGameServer
{
//继承 ApplicationBase
public class MyGameServer : ApplicationBase
{
//日志打印
public static readonly ILogger log = LogManager.GetCurrentClassLogger(); public new static MyGameServer Instance { get; private set; } public Dictionary<OperationCode,BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>(); //客户端创建链接
//使用一个peerbase表示一个客户端连接
protected override PeerBase CreatePeer(InitRequest initRequest)
{
log.Info("Client Connect----------");
//创建一个客户端返回给引擎,引擎自动管理
return new ClientPeer(initRequest);
} //服务器启动时调用
protected override void Setup()
{
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(ApplicationRootPath, "log");
GlobalContext.Properties["LogFileName"] = "MySer_" + ApplicationName;
XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(BinaryPath, "log4net.config"))); log.Info("MyGameServer start----------"); Instance = this;
InitHandler();
} private void InitHandler()
{
LoginHander loginHander = new LoginHander();
HandlerDict.Add(loginHander.OpCode,loginHander); DefaultHandler defaultHandler = new DefaultHandler();
HandlerDict.Add(defaultHandler.OpCode,defaultHandler); RegisterHandler registerHandler = new RegisterHandler();
HandlerDict.Add(registerHandler.OpCode,registerHandler);
} //服务器停止调用
protected override void TearDown()
{
log.Info("MyGameServer down----------");
}
}
}
五、修改文件ClientPeer.cs,分发请求到每个Handler中取。根据客户端传来的code判断是哪个请求,找到相应的Handler,然后将上传的参数分发给该Handler进行逻辑处理。
using System.Collections.Generic;
using Common;
using Common.Toos;
using MyGameServer.Hander;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces; namespace MyGameServer
{ public class ClientPeer : Photon.SocketServer.ClientPeer
{
//创建客户端
public ClientPeer(InitRequest initRequest) : base(initRequest)
{
} protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
MyGameServer.log.Info("Client---请求了---" + operationRequest.OperationCode); //首先获取客户端传来的code (operationRequest.OperationCode)
//然后根据code 去 MyGameServer中获取注册的Handler。
//Handler我们注册到了主函数HandlerDict中。
//DictTool工具类是我们自己定义的,方便传入key,就能从Dict中取值,这里取出的是code相对应的handler
BaseHandler handler = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict,
(OperationCode) operationRequest.OperationCode); if (handler != null)
{
//找到相应的Handler,直接调用 OnOperationRequest 进行相应逻辑处理
handler.OnOperationRequest(operationRequest,sendParameters,this);
}
else
{
//如果没有找到,返回我们自定义的 DefaultHandler.
BaseHandler defHander = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict,
OperationCode.Default); defHander.OnOperationRequest(operationRequest,sendParameters,this);
} } //处理客户端断开连接
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
MyGameServer.log.Info("Client------断开了");
}
}
}
到此,服务端代码整理完成。下面进行前端处理。
Photon Server 实现注册与登录(二) --- 服务端代码整理的更多相关文章
- Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册
前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...
- Photon Server 实现注册与登录(五) --- 服务端、客户端完整代码
客户端代码:https://github.com/fotocj007/PhotonDemo_Client 服务端代码:https://github.com/fotocj007/PhotonDemo_s ...
- Photon Server 实现注册与登录(一) --- Hibernate整合到项目中
本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...
- Photon Server 实现注册与登录(三) --- 前端UI设计和发起请求
一.打开之前的测试项目.先将服务端代码编译一下,在 bin/Debug/目录下会发现有一个Common.dill.我们相应导入到前端使用.直接拖拽到相应地方 UI相应布局属于前端操作,这里就不做介绍了 ...
- [Java聊天室server]实战之五 读写循环(服务端)
前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...
- openssl实现双向认证教程(服务端代码+客户端代码+证书生成)
一.背景说明 1.1 面临问题 最近一份产品检测报告建议使用基于pki的认证方式,由于产品已实现https,商量之下认为其意思是使用双向认证以处理中间人形式攻击. <信息安全工程>中接触过 ...
- 使用 git post-receive 钩子部署服务端代码
在 git 中提交服务器源码的时候,如果能够直接更新到测试服务器,并且重启服务使其生效,会节省懒惰的程序员们大量的时间. git 的 Server-side hook (服务端钩子/挂钩)可以用来做件 ...
- 根据wsdl,apache cxf的wsdl2java工具生成客户端、服务端代码
根据wsdl,apache cxf的wsdl2java工具生成客户端.服务端代码 apache cxf的wsdl2java工具的简单使用: 使用步骤如下: 一.下载apache cxf的包,如apac ...
- Socket通信客户端和服务端代码
这两天研究了下Socket通信,简单实现的客户端和服务端代码 先上winfrom图片,客户端和服务端一样 服务端代码: using System; using System.Collections.G ...
随机推荐
- fiddler在小米8下抓取https数据包.
问题,在小米8下一直报 证书链问题,爬了半天帖子发现可能是Android版本问题,有的说用Charles没问题. 没有测试,网上接着爬帖子... 稍稍说下导入证书的问题吧. 可以使用浏览器下载证书,也 ...
- windows游戏编程X86实模式和保护模式
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22309359 作者:jadeshu 邮箱: jades ...
- 2019.6.28 校内测试 T4 【音乐会】达拉崩吧·上
考试的一道附加题~ 一看题目描述:把区间[l,r]里每个数异或上x,求区间[l,r]里所有数的异或和,这明显的要用数据结构或RMQ吧. 恩,所以正解就是线段树啦,至于树状数组行与否,不知道~ wate ...
- 十八、centos7网络属性配置
一.为什么需要这个 服务器通常有多块网卡,有板载集成的,同时也有插在PCIe插槽的.Linux系统的命名原来是eth0,eth1这样的形式,但是这个编号往往不一定准确对应网卡接口的物理顺序.为解决这类 ...
- MGR---mysql组复制多主模式
组复制有两种模式:单主模式和多主模式. 1.在单主模式下,组复制具有自动选主功能,每次只有一个 server成员接受更新.2.在多主模式下,所有的 server 成员都可以同时接受更新. MGR的限制 ...
- Raspberry Pi 摄像头模块入门
目录 一.摄像头模块安装 二.使用命令控制摄像头 三.使用Python程序控制摄像头 四.基于vlc的Raspberry Pi摄像头实时监控 参考资料 Raspberry Pi提供了摄像头模块的接口, ...
- Python __dict__和vars()
1 __dict__ 设想这样一个场景.有一个字典,从某个地方获取的,比如http请求发过来的,比如从redis中hgetall出来的.我要根据这个字典来构建一个对象. 比如类 class Perso ...
- Maven:element '******' cannot have character [children]
此错误是由于XML文件的解析不正确造成的,因为在一个/某些标签之间存在奇怪和隐藏的字符. 这些字符可能来自网络上的复制粘贴.要解决此问题,请删除标签>标记定义之间的所有空格和换行符,然后将它们放 ...
- Oracle JDBC 标准连接实例
Oracle JDBC 标准连接实例 // 创建一个数据库连接 Connection con = null; // 创建预编译语句对象,一般用PreparedStatement不用Statement ...
- es搭建过程会存在的问题 针对6.x
常见的四个基本错误 错误1 can not run elasticsearch as root 解决方案: 因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户 第一步 ...