- 浏览: 264093 次
- 性别:
- 来自: 济南
文章分类
最新评论
unity基础开发----Photon服务器引擎 入门教程二
上一讲中主要介绍了服务器的简单知识,配置服务器和客户端连接.
第二讲介绍客户端请求服务器,服务器响应操作,我们就以一个简单的用户登录为基础介绍吧
一、服务器端
按照上一篇教程我们配置好简单的photon服务器,但是只能用于连接服务器和断开服务器操作,其他的基本没有提到,今天是要在上一讲基础上添加内容.
主要是在MyPeer.cs类的OnOperationRequest方法中实现,代码如下:
- using System;
- using System.Collections.Generic;
- using Photon.SocketServer;
- using PhotonHostRuntimeInterfaces;
- namespace MyServer
- {
- using Message;
- using System.Collections;
- public class MyPeer : PeerBase
- {
- Hashtable userTable;
- public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)
- : base(protocol, photonPeer)
- {
- userTable = new Hashtable();
- userTable.Add("user1","pwd1");
- userTable.Add("user2", "pwd2");
- userTable.Add("user3","pwd3");
- userTable.Add("user4", "pwd4");
- userTable.Add("user5","pwd5");
- }
- protected overridevoid OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode,string reasonDetail)
- {
- }
- protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
- {
- switch (operationRequest.OperationCode) {
- case (byte)OpCodeEnum.Login:
- string uname = (string)operationRequest.Parameters[(byte)OpKeyEnum.UserName];
- string pwd = (string)operationRequest.Parameters[(byte)OpKeyEnum.PassWord];
- if (userTable.ContainsKey(uname) && userTable[uname].Equals(pwd))//login success
- {
- SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginSuccess,null),new SendParameters());
- }
- else
- { //login fauled
- SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginFailed,null), new SendParameters());
- }
- break;
- }
- }
- }
- }
using System; using System.Collections.Generic; using Photon.SocketServer; using PhotonHostRuntimeInterfaces; namespace MyServer { using Message; using System.Collections; public class MyPeer : PeerBase { Hashtable userTable; public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer) : base(protocol, photonPeer) { userTable = new Hashtable(); userTable.Add("user1", "pwd1"); userTable.Add("user2", "pwd2"); userTable.Add("user3", "pwd3"); userTable.Add("user4", "pwd4"); userTable.Add("user5", "pwd5"); } protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail) { } protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { switch (operationRequest.OperationCode) { case (byte)OpCodeEnum.Login: string uname = (string)operationRequest.Parameters[(byte)OpKeyEnum.UserName]; string pwd = (string)operationRequest.Parameters[(byte)OpKeyEnum.PassWord]; if (userTable.ContainsKey(uname) && userTable[uname].Equals(pwd))//login success { SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginSuccess, null),new SendParameters()); } else { //login fauled SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginFailed, null), new SendParameters()); } break; } } } }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MyServer.Message
- {
- enum OpCodeEnum : byte
- {
- //login
- Login = 249,
- LoginSuccess = 248,
- LoginFailed = 247,
- //room
- Create = 250,
- Join = 255,
- Leave = 254,
- RaiseEvent = 253,
- SetProperties = 252,
- GetProperties = 251
- }
- enum OpKeyEnum : byte
- {
- RoomId = 251,
- UserName = 252,
- PassWord = 253
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyServer.Message { enum OpCodeEnum : byte { //login Login = 249, LoginSuccess = 248, LoginFailed = 247, //room Create = 250, Join = 255, Leave = 254, RaiseEvent = 253, SetProperties = 252, GetProperties = 251 } enum OpKeyEnum : byte { RoomId = 251, UserName = 252, PassWord = 253 } }
二、客户端
客户端过程需要请求服务器并接收服务器的响应下面上代码,就一个类搞定:
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using ExitGames.Client.Photon;
- public class TestConnection : MonoBehaviour,IPhotonPeerListener {
- public string address;
- PhotonPeer peer;
- ClientState state = ClientState.DisConnect;
- string username ="";
- string password = "";
- // Use this for initialization
- void Start () {
- peer = new PhotonPeer(this,ConnectionProtocol.Udp);
- }
- // Update is called once per frame
- void Update () {
- peer.Service();
- }
- public GUISkin skin;
- void OnGUI(){
- GUI.skin = skin;
- switch(state){
- case ClientState.DisConnect:
- GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"click the button to connect.");
- if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,100,30),"Connect")){
- peer.Connect(address,"MyServer");
- state = ClientState.Connecting;
- }
- break;
- case ClientState.Connecting:
- GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connecting to Server...");
- break;
- case ClientState.Connected:
- GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));
- //
- GUILayout.BeginVertical();
- GUILayout.Label("Connect Success! Please Login.");
- //draw username
- GUILayout.BeginHorizontal();
- GUILayout.Label("UserName:");
- username = GUILayout.TextField(username);
- GUILayout.EndVertical();
- //draw password
- GUILayout.BeginHorizontal();
- GUILayout.Label("Password:");
- password = GUILayout.TextField(password);
- GUILayout.EndVertical();
- //draw buttons
- GUILayout.BeginHorizontal();
- if(GUILayout.Button("login")){
- userLogin(username,password);
- }
- if(GUILayout.Button("canel")){
- state = ClientState.DisConnect;
- }
- GUILayout.EndVertical();
- GUILayout.EndVertical();
- GUILayout.EndArea();
- break;
- case ClientState.ConnectFailed:
- GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connect Failed.");
- break;
- case ClientState.LoginSuccess:
- GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));
- GUILayout.Label("Login Success!");
- GUILayout.EndArea();
- break;
- case ClientState.LoginFailed:
- GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));
- GUILayout.Label("Login Failed!");
- GUILayout.EndArea();
- break;
- }
- }
- #region My Method
- IEnumerator connectFailedHandle(){
- yield return new WaitForSeconds(1);
- state = ClientState.DisConnect;
- }
- void userLogin(string uname,string pwd){
- Debug.Log("userLogin");
- Dictionary<byte,object> param =new Dictionary<byte,object>();
- param.Add((byte)OpKeyEnum.UserName,uname);
- param.Add((byte)OpKeyEnum.PassWord,pwd);
- peer.OpCustom((byte)OpCodeEnum.Login,param,true);
- }
- IEnumerator loginFailedHandle(){
- yield return new WaitForSeconds(1);
- Debug.Log("loginFailedHandle");
- state = ClientState.Connected;
- }
- #endregion
- #region IPhotonPeerListener implementation
- public void DebugReturn (DebugLevel level,string message)
- {
- }
- public void OnOperationResponse (OperationResponse operationResponse)
- {
- switch(operationResponse.OperationCode)
- {
- case (byte)OpCodeEnum.LoginSuccess:
- Debug.Log("login success!");
- state = ClientState.LoginSuccess;
- break;
- case (byte)OpCodeEnum.LoginFailed:
- Debug.Log("login Failed!");
- state = ClientState.LoginFailed;
- StartCoroutine(loginFailedHandle());
- break;
- }
- }
- public void OnStatusChanged (StatusCode statusCode)
- {
- switch(statusCode){
- case StatusCode.Connect:
- Debug.Log("Connect Success! Time:"+Time.time);
- state = ClientState.Connected;
- break;
- case StatusCode.Disconnect:
- state = ClientState.ConnectFailed;
- StartCoroutine(connectFailedHandle());
- Debug.Log("Disconnect! Time:"+Time.time);
- break;
- }
- }
- public void OnEvent (EventData eventData)
- {
- }
- #endregion
- }
- public enum ClientState :byte{
- DisConnect,
- Connecting,
- Connected,
- ConnectFailed,
- LoginSuccess,
- LoginFailed
- }
- public enum OpCodeEnum :byte
- {
- //login
- Login = 249,
- LoginSuccess = 248,
- LoginFailed = 247,
- //room
- Create = 250,
- Join = 255,
- Leave = 254,
- RaiseEvent = 253,
- SetProperties = 252,
- GetProperties = 251
- }
- public enum OpKeyEnum :byte
- {
- RoomId = 251,
- UserName = 252,
- PassWord = 253
- }
using UnityEngine; using System.Collections; using System.Collections.Generic; using ExitGames.Client.Photon; public class TestConnection : MonoBehaviour,IPhotonPeerListener { public string address; PhotonPeer peer; ClientState state = ClientState.DisConnect; string username = ""; string password = ""; // Use this for initialization void Start () { peer = new PhotonPeer(this,ConnectionProtocol.Udp); } // Update is called once per frame void Update () { peer.Service(); } public GUISkin skin; void OnGUI(){ GUI.skin = skin; switch(state){ case ClientState.DisConnect: GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"click the button to connect."); if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,100,30),"Connect")){ peer.Connect(address,"MyServer"); state = ClientState.Connecting; } break; case ClientState.Connecting: GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connecting to Server..."); break; case ClientState.Connected: GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400)); // GUILayout.BeginVertical(); GUILayout.Label("Connect Success! Please Login."); //draw username GUILayout.BeginHorizontal(); GUILayout.Label("UserName:"); username = GUILayout.TextField(username); GUILayout.EndVertical(); //draw password GUILayout.BeginHorizontal(); GUILayout.Label("Password:"); password = GUILayout.TextField(password); GUILayout.EndVertical(); //draw buttons GUILayout.BeginHorizontal(); if(GUILayout.Button("login")){ userLogin(username,password); } if(GUILayout.Button("canel")){ state = ClientState.DisConnect; } GUILayout.EndVertical(); GUILayout.EndVertical(); GUILayout.EndArea(); break; case ClientState.ConnectFailed: GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connect Failed."); break; case ClientState.LoginSuccess: GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400)); GUILayout.Label("Login Success!"); GUILayout.EndArea(); break; case ClientState.LoginFailed: GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400)); GUILayout.Label("Login Failed!"); GUILayout.EndArea(); break; } } #region My Method IEnumerator connectFailedHandle(){ yield return new WaitForSeconds(1); state = ClientState.DisConnect; } void userLogin(string uname,string pwd){ Debug.Log("userLogin"); Dictionary<byte,object> param = new Dictionary<byte, object>(); param.Add((byte)OpKeyEnum.UserName,uname); param.Add((byte)OpKeyEnum.PassWord,pwd); peer.OpCustom((byte)OpCodeEnum.Login,param,true); } IEnumerator loginFailedHandle(){ yield return new WaitForSeconds(1); Debug.Log("loginFailedHandle"); state = ClientState.Connected; } #endregion #region IPhotonPeerListener implementation public void DebugReturn (DebugLevel level, string message) { } public void OnOperationResponse (OperationResponse operationResponse) { switch(operationResponse.OperationCode) { case (byte)OpCodeEnum.LoginSuccess: Debug.Log("login success!"); state = ClientState.LoginSuccess; break; case (byte)OpCodeEnum.LoginFailed: Debug.Log("login Failed!"); state = ClientState.LoginFailed; StartCoroutine(loginFailedHandle()); break; } } public void OnStatusChanged (StatusCode statusCode) { switch(statusCode){ case StatusCode.Connect: Debug.Log("Connect Success! Time:"+Time.time); state = ClientState.Connected; break; case StatusCode.Disconnect: state = ClientState.ConnectFailed; StartCoroutine(connectFailedHandle()); Debug.Log("Disconnect! Time:"+Time.time); break; } } public void OnEvent (EventData eventData) { } #endregion } public enum ClientState : byte{ DisConnect, Connecting, Connected, ConnectFailed, LoginSuccess, LoginFailed } public enum OpCodeEnum : byte { //login Login = 249, LoginSuccess = 248, LoginFailed = 247, //room Create = 250, Join = 255, Leave = 254, RaiseEvent = 253, SetProperties = 252, GetProperties = 251 } public enum OpKeyEnum : byte { RoomId = 251, UserName = 252, PassWord = 253 }
转载地址:http://blog.csdn.net/asd237241291/article/details/8820166
推荐博客:http://www.cnblogs.com/liusuqi/category/447143.html
相关推荐
高通AR 最新版 vuforia-unity-6-2-10 vuforia-unity-6-2-10
vuforia-unity-6-0-112.unitypackage
vuforia-samples-advanced-android-ios-unity-4-2-3.zip官网下载太慢了,放在网盘方便大家下载
Unity日志输出插件,在手机和pc上均有用,能很清晰的看到日志打印,非常好用。 使用这个工具,你可以很容易地在游戏内部检查你的编辑器控制台日志!在移动端同样有效! 支持unity2017.4.29 or 更高版本
Photon服务器引擎 入门教程一 客户端连接服务器,客户端状态变化
包含了客户端登陆服务器,接收服务器响应等
Unity和C#-游戏开发-官方超好画质的射击游戏源码+源代码工程
Unity 免ADB真机日志查看插件分享Unity Logs Viewe。详情参看https://blog.csdn.net/wuming2016/article/details/90609787
unity-webview-integration-master unity-webview-integration-master unity-webview-integration-master
vuforia-unity-android-modeltargetstest-10-5-5
vuforia-samples-core-unity-6-2-6 These samples show how to build apps using the following core features of Vuforia. Image Targets VuMark Object Recognition Cylinder Targets Multi Targets User ...
Unity真机调试编辑器(Unity-Logs-Viewer-master),兼容iOS和Andriod系统边玩游戏面板显示Debug信息,非常方便;
spine-unity-3.8-2021-01-08.unitypackage
为了速度慢下载失败或者无法下载的人提供,这是最新的vuforia unity sdk 6.2.10
Unity和C#-游戏开发-简单的3D回合制游戏教程+源代码工程
unity spain 3.8 2021-11-10
Unity游戏源码 , Unity工具 , 完整的项目 , 适合学习和二次开发 。 是整个完整的UnityPackage包 , 直接导入项目里面就行。 C#语言的! C#语言的!! C#语言的!!! 重要的事说三遍!!!! 直接创建空项目导入打开就行了,无需...
Unity-UI-Extensions,总有一款效果是你想要的!
UnityDownloadAssistant-2019.4.11f1+unity hub-4.2.1安装包