Photon Server 实现注册与登录(三) --- 前端UI设计和发起请求
一、打开之前的测试项目。先将服务端代码编译一下,在 bin/Debug/目录下会发现有一个Common.dill。我们相应导入到前端使用。直接拖拽到相应地方

UI相应布局属于前端操作,这里就不做介绍了。详细查看视频:https://www.bilibili.com/video/av35109390/?p=70
二、代码处理:跟后端一样,前端发起请求的地方也会随着项目变大而变多,所以也得单独区分出每个请求独自管理。
Request.cs 请求基类
LoginRequest.cs 登录请求逻辑
RegiesterRequest.cs 注册请求连接 LoginPanel.cs 登录面挂载脚本
ReginsterPanel.cs 注册脚本挂载脚

三、调整PhotonManager.cs 管理类
(1)、添加字典,管理所以请求
private Dictionary<OperationCode,Request> ResquesDict = new Dictionary<OperationCode, Request>();
(2)、分配peer
public static PhotonPeer Peer
{
get { return peer; }
}
(3)、添加两个函数,管理请求
public void AddRequest(Request rest)
{
ResquesDict.Add(rest.OpCode,rest);
} public void RemoveRequest(Request rest)
{
ResquesDict.Remove(rest.OpCode);
}
(4)、修改OnOperationResponse() 函数,实现接收服务端返回数据的分发
/// <summary>
/// 客户端请求后,服务端响应,返回数据
/// </summary>
/// <param name="operationResponse"></param>
public void OnOperationResponse(OperationResponse operationResponse)
{
//服务端返回的code
OperationCode opCode = (OperationCode)operationResponse.OperationCode;
Request request = null; //根据code查找相应请求
bool tmp = ResquesDict.TryGetValue(opCode, out request); if (tmp)
{
//如果存在,分发给相应OnOperationResponse去处理自己的接收数据逻辑
request.OnOperationResponse(operationResponse);
}
else
{
Debug.Log("没找到响应处理对象");
}
}
(5)、完整代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using Common;
using UnityEngine;
using ExitGames.Client.Photon; public class PhotonManager : MonoBehaviour,IPhotonPeerListener
{
//单例模式,保证只有一个链接服务器
public static PhotonManager Instance;
private static PhotonPeer peer;
private bool connected;
private Dictionary<OperationCode,Request> ResquesDict = new Dictionary<OperationCode, Request>();
public static PhotonPeer Peer
{
get { return peer; }
} private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this.gameObject);
}else if (Instance != this)
{
Destroy(this.gameObject);
return;
}
} // Start is called before the first frame update
void Start()
{
Debug.Log("111------开始连接----"); peer = new PhotonPeer(this, ConnectionProtocol.Udp);
peer.Connect("127.0.0.1:5055","MyGame1");
connected = false;
} // Update is called once per frame
void Update()
{
Debug.Log("update------" + connected); peer.Service();
} private void OnDestroy()
{
if (peer != null && peer.PeerState == PeerStateValue.Connected)
{
peer.Disconnect();
}
} public void DebugReturn(DebugLevel level, string message)
{
} /// <summary>
/// 客户端请求后,服务端响应,返回数据
/// </summary>
/// <param name="operationResponse"></param>
public void OnOperationResponse(OperationResponse operationResponse)
{
//服务端返回的code
OperationCode opCode = (OperationCode)operationResponse.OperationCode;
Request request = null; //根据code查找相应请求
bool tmp = ResquesDict.TryGetValue(opCode, out request); if (tmp)
{
//如果存在,分发给相应OnOperationResponse去处理自己的接收数据逻辑
request.OnOperationResponse(operationResponse);
}
else
{
Debug.Log("没找到响应处理对象");
}
} /// <summary>
/// 连接状态发生改变时
/// </summary>
/// <param name="statusCode"></param>
public void OnStatusChanged(StatusCode statusCode)
{
Debug.Log("serStatus-----" + statusCode.ToString()); switch (statusCode)
{
case StatusCode.Connect:
connected = true;
break;
default:
connected = false;
break;
}
} /// <summary>
/// 服务端直接给客户端数据时,不需要向服务器请求
/// </summary>
/// <param name="eventData"></param>
public void OnEvent(EventData eventData)
{
switch (eventData.Code)
{
case :
break;
case : break;
default:
break;
}
} public void AddRequest(Request rest)
{
ResquesDict.Add(rest.OpCode,rest);
} public void RemoveRequest(Request rest)
{
ResquesDict.Remove(rest.OpCode);
}
}
四、请求类
(1)、Request.cs 基类
using System;
using Common;
using ExitGames.Client.Photon;
using UnityEngine; public abstract class Request: MonoBehaviour
{
//定义请求code
public OperationCode OpCode; public abstract void DefaultRequest(); //发送请求 public abstract void OnOperationResponse(OperationResponse operationResponse); //接收返回的数据 public virtual void Start()
{
//从管理类中添加该请求
PhotonManager.Instance.AddRequest(this);
} public void OnDestroy()
{
//从管理类中移除该请求
PhotonManager.Instance.RemoveRequest(this);
}
}
(2)、登陆类 LoginRequest.cs
using System.Collections.Generic;
using Common;
using ExitGames.Client.Photon;
using UnityEngine; public class LoginRequest : Request
{
//通过代码赋值,所有在UI界面上隐藏掉
[HideInInspector]
public string UserName;
[HideInInspector]
public string Password; //获取UI面板
private LoginPanel loginPanel; public override void Start()
{
//调用基类Start,将登陆请求添加到管理类中
base.Start();
loginPanel = GetComponent<LoginPanel>();
}
public override void DefaultRequest()
{
//登录请求发起
Dictionary<byte,object> data = new Dictionary<byte, object>();
data.Add((byte) ParameterCode.UserName,UserName);
data.Add((byte) ParameterCode.Password,Password); //通过Peer发送请求给服务端
PhotonManager.Peer.OpCustom((byte)OpCode,data,true);
} public override void OnOperationResponse(OperationResponse operationResponse)
{
//接收服务端的返回响应,将返回code给面板,由面板来处理接下来的逻辑(是提示错误还是跳转场景)
ReturnCode returnCode = (ReturnCode) operationResponse.ReturnCode;
loginPanel.OnLiginResponse(returnCode);
}
}
(3)、登陆面板脚本 LoginPannel.cs
using System;
using System.Collections;
using System.Collections.Generic;
using Common;
using UnityEngine;
using UnityEngine.UI; public class LoginPanel : MonoBehaviour
{
//以下public参数通过UI拖拽进行赋值
#region Tags //注册面板,用于点击注册时显示该面板
public GameObject registerPanel;
//控件
public InputField username;
public InputField password;
public Text hintmessage; //登陆请求类
private LoginRequest loginRequest; #endregion private void Start()
{
//从面板中获取请求类(面板脚本和请求类都挂载到请求面板UI中)
loginRequest = GetComponent<LoginRequest>();
} public void OnLoginButton()
{
//点击登陆按钮时,发送请求
hintmessage.text = ""; loginRequest.UserName = username.text;
loginRequest.Password = password.text; //调用请求类中的发起请求
loginRequest.DefaultRequest();
} public void OnRegisterButton()
{
//点击注册时,隐藏登陆UI,显示注册UI
gameObject.SetActive(false);
registerPanel.SetActive(true);
} public void OnLiginResponse(ReturnCode returnCode)
{
//服务端响应返回数据时,请求类会将返回信息传递到该函数
Debug.Log("-----返回code" + returnCode); if (returnCode == ReturnCode.Success)
{
//登录成功,调整下一个场景
Debug.Log("--登陆成功----");
}
else
{
hintmessage.text = "用户名或密码错误";
}
}
}
注册类和注册面板跟登陆相同逻辑
(4)、注册类 ReginesterRequest.cs
using System.Collections;
using System.Collections.Generic;
using Common;
using ExitGames.Client.Photon;
using UnityEngine; public class RegiesterRequet : Request
{
[HideInInspector]
public string username;
[HideInInspector]
public string password; private ReginsterPanel reginsterPanel; public override void Start()
{
base.Start();
reginsterPanel = GetComponent<ReginsterPanel>();
} public override void DefaultRequest()
{
//登录请求发起
Dictionary<byte,object> data = new Dictionary<byte, object>();
data.Add((byte) ParameterCode.UserName,username);
data.Add((byte) ParameterCode.Password,password); PhotonManager.Peer.OpCustom((byte)OpCode,data,true);
} public override void OnOperationResponse(OperationResponse operationResponse)
{
ReturnCode returnCode = (ReturnCode) operationResponse.ReturnCode;
reginsterPanel.OnReigsterResponse(returnCode);
} }
(5)、注册面板 ReginsterPanel.cs
using System;
using System.Collections;
using System.Collections.Generic;
using Common;
using UnityEngine;
using UnityEngine.UI; public class ReginsterPanel : MonoBehaviour
{
public GameObject loginPanel; public InputField username;
public InputField password;
public Text hintmessage; private RegiesterRequet regiesterRequet; private void Start()
{
regiesterRequet = GetComponent<RegiesterRequet>();
} public void OnRegisterButton()
{
hintmessage.text = ""; regiesterRequet.username = username.text;
regiesterRequet.password = password.text; regiesterRequet.DefaultRequest();
} public void OnBackBtn()
{
loginPanel.SetActive(true);
gameObject.SetActive(false);
} public void OnReigsterResponse(ReturnCode returnCode)
{
if (returnCode == ReturnCode.Success)
{
hintmessage.text = "注册成功,请返回登录";
}
else
{
hintmessage.text = "用户名重复,请重新修改用户名";
}
}
}

前端UI和请求、响应逻辑基本完成。接下来进行服务端响应。
查看视频:https://www.bilibili.com/video/av35109390/?p=70
Photon Server 实现注册与登录(三) --- 前端UI设计和发起请求的更多相关文章
- Photon Server 实现注册与登录(一) --- Hibernate整合到项目中
本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...
- Photon Server 实现注册与登录(二) --- 服务端代码整理
一.有的代码前端和后端都会用到.比如一些请求的Code.使用需要新建项目存放公共代码. 新建项目Common存放公共代码: EventCode :存放服务端自动发送信息给客户端的code Operat ...
- Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册
前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...
- Photon Server 实现注册与登录(五) --- 服务端、客户端完整代码
客户端代码:https://github.com/fotocj007/PhotonDemo_Client 服务端代码:https://github.com/fotocj007/PhotonDemo_s ...
- Photon Server与Unity3D客户端的交互
Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...
- SpringBoot注册登录(三):注册--验证账号密码是否符合格式及后台完成注册功能
SpringBoot注册登录(一):User表的设计点击打开链接SpringBoot注册登录(二):注册---验证码kaptcha的实现点击打开链接 SpringBoot注册登录(三):注册 ...
- 实战django(一)--(你也能看懂的)注册与登录(带前端模板)
先是具体目录:(主要是注意templates和static的位置),其中person文件夹是上一期实战的,不用理会,login是本节实战app 项目urls.py from django.contri ...
- Halo 开源项目学习(三):注册与登录
基本介绍 首次启动 Halo 项目时需要安装博客并注册用户信息,当博客安装完成后用户就可以根据注册的信息登录到管理员界面,下面我们分析一下整个过程中代码是如何执行的. 博客安装 项目启动成功后,我们可 ...
- 基于 Socket 的群聊聊天室(带图形界面,包含注册、登录、数据入库功能)
代码下载 https://github.com/juno3550/GroupChatRoom 实现框架 Chat 包: server.py:服务器端执行代码(TCP 服务器,根据客户端消息调用 mod ...
随机推荐
- ORM SQLAlchemy 简介
对象关系映射(Object Relational Mapping,简称ORM使用DB-API访问数据库,需要懂 SQL 语言,能够写 SQL 语句,如果不想懂 SQL,又想使用关系型数据库,可以使用 ...
- go之构造体方法
package main import ( "fmt" "math" ) type Vertexs struct { X, Y float64 } //Abs ...
- DLL:操作数据库和表
1. 操作数据库 C(Create 创建) R(Retrieve 查询) U(Update 更新) D(Delete 删除) (1) 查询数据库 1) 查询所有数据库名称 SHOW DATABASES ...
- Oracle JDBC 标准连接实例
Oracle JDBC 标准连接实例 // 创建一个数据库连接 Connection con = null; // 创建预编译语句对象,一般用PreparedStatement不用Statement ...
- Mac下持续集成-Jenkins权限设置
部署上后集成Jmeter玩了一晚上,后来发现账号登录不进去了,
- Jmeter启动闪退
背景:使用Jmeter进行性能测试脚本开发,实现Jmeter接口开发Java请求 问题再现: 1.在官网上下载Jmeter3.2版本,打开jmeter.bat,启动成功 2.在IDEA中创建测试项目和 ...
- Java NIO学习笔记四 NIO选择器
Java NIO选择器 A Selector是一个Java NIO组件,可以检查一个或多个NIO通道,并确定哪些通道已准备就绪,例如读取或写入.这样一个线程可以管理多个通道,从而管理多个网络连接. 为 ...
- hadoop 2.8.5安装步骤
1.创建hadoop用户,作为haoop的运行用户 2.配置JAVA_HOME环境变量,修改/etc/profile export JAVA_HOME=/usr/java/jdk1.8.0_51 ex ...
- Python 利用二分法查询数据
一. 二分法的适用条件 二分法查找适用于数据量较大时, 但是数据需要先排好顺序. 优点: 二分法查找效率特别高 缺点: 二分法只适用于有序序列 二. 二分法的主要思想是:设查找的数组区间为array[ ...
- Hadoop 3.1.3伪分布式环境安装Hive 3.1.2的异常总结
背景:hadoop版本为3.1.3, 且以伪分布式形式安装,hive版本为3.1.2,hive为hadoop的一个客户端. 1. 安装简要步骤 (1) 官网下载apache-hive-3.1.2-bi ...