文章目录
- Unity进阶--通过PhotonServer实现联网登录注册功能(服务器端)--PhotonServer(二)
- 服务器端
- 大体结构图
- BLL层(控制层)
- DAL层(数据控制层)
- 模型层
- DLC 服务器配置类 发送消息类 以及消息类
Unity进阶–通过PhotonServer实现联网登录注册功能(服务器端)–PhotonServer(二)
如何配置PhotonServer服务器:https://blog.csdn.net/abaidaye/article/details/132096415
服务器端
大体结构图
- 结构图示意
BLL层(控制层)
-
总管理类
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace PhotonServerFirst.Bll {public class BLLManager{private static BLLManager bLLManager;public static BLLManager Instance{get{if(bLLManager == null){bLLManager = new BLLManager();}return bLLManager;}}//登录注册管理public IMessageHandler accountBLL;private BLLManager(){accountBLL = new Account.AccountBLL();}} }
-
控制层接口
using Net;namespace PhotonServerFirst.Bll {public interface IMessageHandler{//处理客户端断开的后续工作void OnDisconnect(PSpeer peer);//处理客户端的请求void OnOperationRequest(PSpeer peer, PhotonMessage message);} }
-
登录注册控制类
using Net; using PhotonServerFirst.Dal;namespace PhotonServerFirst.Bll.Account {class AccountBLL : IMessageHandler{public void OnDisconnect(PSpeer peer){throw new System.NotImplementedException();}public void OnOperationRequest(PSpeer peer, PhotonMessage message){//判断命令switch (message.Command){case MessageType.Account_Register:Register(peer, message);break;case MessageType.Account_Login:Login(peer, message);break;}}//注册请求 0账号1密码void Register(PSpeer peer, PhotonMessage message){object[] objs = (object[])message.Content;//添加用户int res = DAlManager.Instance.accountDAL.Add((string)objs[0],(string)objs[1]);//服务器响应SendMessage.Send(peer, MessageType.Type_Account, MessageType.Account_Register_Res, res);}//登陆请求 0账号1密码void Login(PSpeer peer, PhotonMessage message){object[] objs = (object[])message.Content;//登录int res = DAlManager.Instance.accountDAL.Login(peer, (string)objs[0], (string)objs[1]);//响应SendMessage.Send(peer, MessageType.Type_Account, MessageType.Account_Login_res, res);}} }
DAL层(数据控制层)
-
总数据管理层
using PhotonServerFirst.Bll; using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace PhotonServerFirst.Dal {class DAlManager{private static DAlManager dALManager;public static DAlManager Instance{get{if (dALManager == null){dALManager = new DAlManager();}return dALManager;}}//登录注册管理public AccountDAL accountDAL;private DAlManager(){accountDAL = new AccountDAL();}} }
-
登录注册数据管理层
using PhotonServerFirst.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace PhotonServerFirst.Dal {class AccountDAL{/// <summary>/// 保存注册的账号/// </summary>private List<AccountModel> accountList = new List<AccountModel>();private int id = 1;///<summary>///保存已经登录的账号/// </summary>private Dictionary<PSpeer, AccountModel> peerAccountDic = new Dictionary<PSpeer, AccountModel>();///<summary>/// 添加账号///</summary>///<param name="account"> 用户名</param>///<param name="password">密码</param>///<returns>1 成功 -1账号已存在 0失败</returns>public int Add(string account, string password){//如果账号已经存在foreach (AccountModel model in accountList){if (model.Account == account){return -1;}}//如果不存在AccountModel accountModel = new AccountModel();accountModel.Account = account;accountModel.Password = password;accountModel.ID = id++;accountList.Add(accountModel);return 1;}/// <summary>/// 登录账号/// </summary>/// <param name="peer">连接对象</param>/// <param name="account">账号</param>/// <param name="password">密码</param>/// <returns>登陆成功返回账号id -1已经登陆 0用户名密码错误</returns>public int Login(PSpeer peer, string account, string password){//是否已经登陆foreach (AccountModel model in peerAccountDic.Values){if (model.Account == account){return -1;}}//判断用户名密码是否正确foreach (AccountModel model in accountList){if (model.Account == account && model.Password == password){peerAccountDic.Add(peer, model);return model.ID;}}return 0;}} }
模型层
-
登录注册层
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace PhotonServerFirst.Model {/// <summary>/// 账号模型/// </summary>class AccountModel{public int ID;public string Account; public string Password;} }
DLC 服务器配置类 发送消息类 以及消息类
-
服务器配置类
using Photon.SocketServer; using ExitGames.Logging; using ExitGames.Logging.Log4Net; using log4net.Config; using System.IO;namespace PhotonServerFirst {public class PSTest : ApplicationBase{//日志需要的public static readonly ILogger log = LogManager.GetCurrentClassLogger();protected override PeerBase CreatePeer(InitRequest initRequest){ return new PSpeer(initRequest);}//初始化protected override void Setup(){InitLog();}//server端关闭的时候protected override void TearDown(){}#region 日志/// <summary>/// 初始化日志以及配置/// </summary>private void InitLog(){//日志的初始化log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = this.ApplicationRootPath + @"\bin_Win64\log";//设置日志的路径FileInfo configFileInfo = new FileInfo(this.BinaryPath + @"\log4net.config");//获取配置文件if (configFileInfo.Exists){//对photonserver设置日志为log4netLogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);XmlConfigurator.ConfigureAndWatch(configFileInfo);log.Info("初始化成功");}}#endregion } }
-
服务器面向客户端类
using System; using System.Collections.Generic; using Net; using Photon.SocketServer; using PhotonHostRuntimeInterfaces; using PhotonServerFirst.Bll;namespace PhotonServerFirst {public class PSpeer : ClientPeer{public PSpeer(InitRequest initRequest) : base(initRequest){}//处理客户端断开的后续工作protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail){//关闭管理器BLLManager.Instance.accountBLL.OnDisconnect(this);}//处理客户端的请求protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters){var dic = operationRequest.Parameters;//转为PhotonMessagePhotonMessage message = new PhotonMessage();message.Type = (byte)dic[0];message.Command = (int)dic[1];List<object> objs = new List<object>();for (byte i = 2; i < dic.Count; i++){objs.Add(dic[i]);}message.Content = objs.ToArray();//消息分发switch (message.Type){case MessageType.Type_Account:BLLManager.Instance.accountBLL.OnOperationRequest(this, message); break;case MessageType.Type_User:break;}}} }
-
消息类
因为这个类是unity和服务器端都需要有的,所以最好生成为
dll
文件放进unity(net3.5以下)namespace Net {public class PhotonMessage{public byte Type;public int Command;public object Content;public PhotonMessage() { }public PhotonMessage(byte type, int command, object content){Type = type;Command = command;Content = content;}}//消息类型public class MessageType{public const byte Type_Account = 1;public const byte Type_User = 2;//注册账号public const int Account_Register = 100;public const int Account_Register_Res = 101;//登陆public const int Account_Login = 102;public const int Account_Login_res = 103;} }
-
发送消息类
using Photon.SocketServer; using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace PhotonServerFirst {class SendMessage{/// <summary>/// 发送消息/// </ summary>/// <param name = "peer"> 连接对象 </ param >/// < param name="type">类型</param>/// <param name="command"> 命令</param>/// <param name = "objs" > 参数 </ param > public static void Send(PSpeer peer, byte type,int command,params object[] objs){Dictionary<byte, object> dic = new Dictionary<byte, object>(); dic.Add(0, type);dic.Add(1, command);byte i = 2;foreach (object o in objs){dic.Add(i++,o);}EventData ed = new EventData(0, dic);peer.SendEvent(ed, new SendParameters());}} }