Unity进阶--通过PhotonServer实现联网登录注册功能(服务器端)--PhotonServer(二)

news2024/9/30 1:41:11

文章目录

  • Unity进阶--通过PhotonServer实现联网登录注册功能(服务器端)--PhotonServer(二)
    • 服务器端
      • 大体结构图
      • BLL层(控制层)
      • DAL层(数据控制层)
      • 模型层
      • DLC 服务器配置类 发送消息类 以及消息类

Unity进阶–通过PhotonServer实现联网登录注册功能(服务器端)–PhotonServer(二)

如何配置PhotonServer服务器:https://blog.csdn.net/abaidaye/article/details/132096415

服务器端

大体结构图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kV5RpqoB-1691151609885)(../AppData/Roaming/Typora/typora-user-images/image-20230804195201056.png)]

  • 结构图示意

未命名文件 (1)

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设置日志为log4net
                    LogManager.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;
    
                //转为PhotonMessage
                PhotonMessage 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());
            }
        }  
    }
    
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/837292.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Gartner发布《2023年全球RPA魔力象限》:90%RPA厂商,将提供生成式AI自动化

8月3日&#xff0c;全球著名咨询调查机构Gartner发布了《2023年全球RPA魔力象限》&#xff0c;通过产品能力、技术创新、市场影响力等维度&#xff0c;对全球16家卓越RPA厂商进行了深度评估。 弘玑Cyclone&#xff08;Cyclone Robotics&#xff09;、来也&#xff08;Laiye&am…

【蓝图】p47下车减速功能

p47下车减速功能 p47下车减速功能加速功能下车减速功能 p47下车减速功能 加速功能 上图是ue自带的加速功能&#xff0c;检测到按w时输入轴会传1给设置油门输入&#xff0c;就会加速 所以&#xff0c;减速也可以通过蓝图反方向制作 下车减速功能 打开Sedan蓝图类的上下车图表…

day51-Mybatis-Plus/代码生成器

1.Mybatis-Plus 定义&#xff1a;是一个Mybatis的增强工具&#xff0c;只在Mybatis基础上增强不做改变&#xff0c;简化开发&#xff0c;提升效率 2.MP实战 2.1 创建springboot工程&#xff0c;勾选web&#xff0c;引入依赖 <dependency> <groupId>mysql<…

人工智能可解释性分析导论(初稿)

目录 思维导图 1.黑箱所带来的问题 2.从应用面论述为什么要进行可解释性分析 2.1可解释性分析指什么 2.2可解释性分析结合人工智能应用实例 2.3 可解释性分析的脑回路&#xff08;以可视化为例如何&#xff09; 3.如何研究可解释性分析 3.1使用好解释的模型 3.2传统机器学…

antDv table组件滚动截图方法的实现

在开发中经常遇到table内容过多产生滚动的场景&#xff0c;正常情况下不产生滚动进行截图就很好实现&#xff0c;一旦产生滚动就会变得有点棘手。 下面分两种场景阐述解决的方法过程 场景一&#xff1a;右侧不固定列的情况 场景二&#xff1a;右侧固定列的情况 场景一 打开…

理解树的结构

树的重要性 二分查找算法、几种核心的排序算法以及图算法都与树有非常密切的关系。有句话锁&#xff0c;“没学会树&#xff0c;算法相当于白学”&#xff0c;可见&#xff0c;树在算法中的地位。 树的考察方面 层次遍历以及拓展问题 前后序遍历与拓展问题 中序遍历与搜索树问…

数据结构入门指南:带头双向循环链表

目录 文章目录 前言 1.结构与优势 2.链表实现 2.1 定义链表 2.2 创建头节点 2.3 尾插 2.4 输出链表 2.5 尾删 2.6 头插 2.7头删 2.8 节点个数 2.9 查找 2.10 位置插入 2.11 位置删除 2.12 销毁链表 3. 源码 总结 前言 链表一共有8种结构&#xff0c;但最常用的就是无头单…

Docker网络模型使用详解(2)Docker网络模式

安装Docker时会自动创建3个网络&#xff0c;可以使用docker network ls命令列出这些网络。 [rootlocalhost ~]# docker network ls NETWORK ID NAME DRIVER SCOPE ebcfad6f4255 bridge bridge local b881c67f8813 compose_lnmp_lnmp…

Vue2升级Vue3报错:Right-hand side of ‘instanceof‘ is not an object

属性prop设置多类型报错&#xff1a; Vue2 写法&#xff1a;支持用竖线隔开。Vue2 Prop expandLevel: {type: Number | String,default: 1, }, Vue3 写法&#xff1a;改为数组&#xff0c;不支持竖线隔开。Vue3 Prop expandLevel: {type: [Number, String],default: 1, }

二次元美少女【InsCode Stable Diffusion 美图活动一期】

目录 Stable Diffusion 模型在线使用地址 一、背景介绍 二、模板介绍&#xff1a; 三、操作步骤 1.在线运行地址 2.进入在线运行网址&#xff0c;并点击运行及使用 3.购买GPU并创建项目 4.打开工作台并选择算力资源 5.点击下图中所示框框 6.进入Stable Diffusion WebU…

VR内容研发公司 | VR流感病毒实验虚拟现实课件

由广州华锐互动开发的《VR流感病毒实验虚拟现实课件》是一种新型的教学模式&#xff0c;可以为学生提供更加真实和直观的流感病毒分离鉴定实验操作体验&#xff0c;从而提高学生的实验技能和工作效率。 《VR流感病毒实验虚拟现实课件》涉及了生物安全二级实验室(BSL-2)和流感病…

.jpeg转.jpg,cv2.resize()

from PIL import Image import os # 定义原文件夹路径和目标文件夹路径 source_folder "path/to/source/folder" target_folder "path/to/target/folder" # 遍历原文件夹中的所有图片文件 for filename in os.listdir(source_folder): if fil…

【iOS安全】安装Filza || 安装Flexdecrypt

&#xff08;成功&#xff09;使用Cydia安装Filza 直接在Cydia里搜索filza&#xff0c;安装“Filza File Manager” 使用Filza安装flexdecrypt 参考&#xff1a; https://github.com/JohnCoates/flexdecrypt 下载flexdecrypt.deb到手机&#xff1a; https://github.com/JohnC…

猎聘:2023届高校毕业生就业数据报告(附下载

关于报告的所有内容&#xff0c;公众【营销人星球】获取下载查看 核心观点 较 2022 届应届生职位同比增长较明显的TOP5 一级行业为能源/化工/环保、医疗健康、汽车、机械/制造、电子/通信/半导体&#xff0c;其中能源/化工/环保同比增长为 42.30%&#xff0c;增速最高.在全世…

mybatisplus实现自动填充 时间

mybatisplus实现自动填充功能——自动填充时间 数据库表中的字段 创建时间 (createTime)更新时间 (updateTime) 每次 增删改查的时候&#xff0c;需要通过对Entity的字段&#xff08;createTime&#xff0c;updateTime&#xff09;进行set设置&#xff0c;但是&#xff0c;每…

Systemui的介绍以及与普通应用的差异

一.SystemUI的介绍 简介 SystemUI是Android操作系统的一个关键组件&#xff0c;主要负责管理和提供用户界面的核心元素&#xff0c;如状态栏、导航栏和锁屏界面等。从下面两点出发了解SystemUI的特性&#xff1a; 一下就是systemui的部分界面&#xff0c;还包括锁屏界面&…

Android Tencent Shadow 插件接入指南

Android Tencent Shadow 插件接入指南 插件化简述一、clone 仓库二、编译运行官方demo三、发布Shadow到我们本地仓库3.1、安装Nexus 3.x版本3.2、修改发布配置3.3、发布仓库3.4、引用仓库包 四、编写我们自己的代码4.1、新建项目导入maven等共同配置4.1.1、导入buildScript4.1.…

NsPack3.x脱壳手记

发现是NsPack3.x的壳 使用ESP守恒快速脱壳 F9遇到popfd后下面的jmp就是通往OEP了 打开LordPE准备转储映像, 首先调整下ImageSize, 接着dump full 接着不要退出目前的调试, 打开Scylla修复IAT, 把OEP的VA地址输入到OEP处, 接着按照如下图所示步骤 完成后如下, 但NsPack3.x…

uni-app、H5实现瀑布流效果封装,列可以自定义

文章目录 前言一、效果二、使用代码三、核心代码总结前言 最近做项目需要实现uni-app、H5实现瀑布流效果封装,网上搜索有很多的例子,但是代码都是不够完整的,下面来封装一个uni-app、H5都能用的代码。在小程序中,一个个item渲染可能出现问题,也通过加锁来解决问题。 一、…

小红书卖虚拟学习资料操作方法超详细讲解,不怕你学不会

科思创业汇 大家好&#xff0c;这里是科思创业汇&#xff0c;一个轻资产创业孵化平台。赚钱的方式有很多种&#xff0c;我希望在科思创业汇能够给你带来最快乐的那一种&#xff01; 谈到赚钱&#xff0c;许多人认为他们赚不到钱&#xff0c;因为他们缺乏赚钱的能力。 然而&a…