一、打开之前的测试项目。先将服务端代码编译一下,在 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设计和发起请求的更多相关文章

  1. Photon Server 实现注册与登录(一) --- Hibernate整合到项目中

    本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...

  2. Photon Server 实现注册与登录(二) --- 服务端代码整理

    一.有的代码前端和后端都会用到.比如一些请求的Code.使用需要新建项目存放公共代码. 新建项目Common存放公共代码: EventCode :存放服务端自动发送信息给客户端的code Operat ...

  3. Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册

    前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...

  4. Photon Server 实现注册与登录(五) --- 服务端、客户端完整代码

    客户端代码:https://github.com/fotocj007/PhotonDemo_Client 服务端代码:https://github.com/fotocj007/PhotonDemo_s ...

  5. Photon Server与Unity3D客户端的交互

    Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...

  6. SpringBoot注册登录(三):注册--验证账号密码是否符合格式及后台完成注册功能

    SpringBoot注册登录(一):User表的设计点击打开链接SpringBoot注册登录(二):注册---验证码kaptcha的实现点击打开链接      SpringBoot注册登录(三):注册 ...

  7. 实战django(一)--(你也能看懂的)注册与登录(带前端模板)

    先是具体目录:(主要是注意templates和static的位置),其中person文件夹是上一期实战的,不用理会,login是本节实战app 项目urls.py from django.contri ...

  8. Halo 开源项目学习(三):注册与登录

    基本介绍 首次启动 Halo 项目时需要安装博客并注册用户信息,当博客安装完成后用户就可以根据注册的信息登录到管理员界面,下面我们分析一下整个过程中代码是如何执行的. 博客安装 项目启动成功后,我们可 ...

  9. 基于 Socket 的群聊聊天室(带图形界面,包含注册、登录、数据入库功能)

    代码下载 https://github.com/juno3550/GroupChatRoom 实现框架 Chat 包: server.py:服务器端执行代码(TCP 服务器,根据客户端消息调用 mod ...

随机推荐

  1. 使用appium+python做UI自动化的demo

    使用appium+python做UI自动化的demo 案例使用的知乎app,下载最新的知乎apk,存在了电脑上,只需要配置本机上app目录,不需要再配置appPackage和appActivity # ...

  2. POJ 2778 DNA Sequence —— (AC自动机+矩阵快速幂)

    距离上次做AC自动机有很久了=.=,以前这题的思路死活看不懂,现在还是觉得很好理解的. 思路参见:http://blog.csdn.net/morgan_xww/article/details/783 ...

  3. 2016"百度之星" - 初赛(Astar Round2A)1001 All X(HDU5690)——找循环节|快速幂

    一个由m个数字x组成的新数字,问其能否mod k等于c. 先提供第一种思路,找循环节.因为每次多一位数都是进行(t*10+x)mod k(这里是同余模的体现),因为x,k都确定,只要t再一样得到的答案 ...

  4. 为win10下的linux子系统终端添加powerline

    一切按照上一篇完成,如果成功了,你厉害了我的哥,如果不成功,win10安装powerline字体才可以,才可以,才可以 sudo apt install build-essential cmake g ...

  5. 异步机制 - ReadFileEx(WriteFileEx)

    1 ReadFileEx定义 BOOL WINAPI ReadFileEx( __in HANDLE hFile, __out LPVOID lpBuffer, __in DWORD nNumberO ...

  6. Java ExecutorService四种线程池及自定义ThreadPoolExecutor机制

    一.Java 线程池 Java通过Executors提供四种线程池,分别为:1.newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...

  7. 深入探索REST(2):理解本真的REST架构风格

    文章转载地址:https://www.infoq.cn/article/understanding-restful-style/,如引用请标注文章原地址 引子 在移动互联网.云计算迅猛发展的今天,作为 ...

  8. Flutter移动电商实战 --(27)列表页_现有Bug修复和完善

    小解决小bug 默认右侧的小类没有被加载 数据加载完成后,就list的第一个子对象传递给provide进行赋值,这样右侧的小类就刷新了数据 默认加载了第一个类别 调整颜色 对比图片调整下颜色 这里的参 ...

  9. [Java复习] JVM

    Part1:Java类加载机制:类加载器,类加载机制,双亲委派模型 1. Java 类加载过程? 类加载过程即是指JVM虚拟机把.class文件中类信息加载进内存,并进行解析生成对应的class对象的 ...

  10. linux---学习3

    1.free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. //-m:以MB为单位显示内存使用情况: free -m 2.vmstat命令的含义为显示虚拟内存状态, ...