Photon服务器引擎 入门教程二
来源:互联网 发布:软件怎么发布 编辑:程序博客网 时间:2024/05/02 00:57
上一讲中主要介绍了服务器的简单知识,配置服务器和客户端连接.
第二讲介绍客户端请求服务器,服务器响应操作,我们就以一个简单的用户登录为基础介绍吧
一、服务器端
按照上一篇教程我们配置好简单的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 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; } } }}OnOperationRequest方法中验证用户名和密码,然后发送响应给客户端.需要用到的枚举一个类如下:
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 initializationvoid Start () {peer = new PhotonPeer(this,ConnectionProtocol.Udp);}// Update is called once per framevoid 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 usernameGUILayout.BeginHorizontal();GUILayout.Label("UserName:");username = GUILayout.TextField(username);GUILayout.EndVertical();//draw passwordGUILayout.BeginHorizontal();GUILayout.Label("Password:");password = GUILayout.TextField(password);GUILayout.EndVertical();//draw buttonsGUILayout.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 MethodIEnumerator 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 implementationpublic 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}
项目源码免积分下载:服务器端和客户端
- Photon服务器引擎 入门教程二
- Photon服务器引擎 入门教程二
- Photon服务器引擎 入门教程二
- Photon服务器引擎 入门教程
- Photon服务器入门教程二
- Photon服务器引擎 入门教程一
- Photon服务器引擎 入门教程一
- Photon服务器引擎 入门教程一
- Photon服务器引擎入门教程一
- unity基础开发----Photon服务器引擎 入门教程二
- unity基础开发----Photon服务器引擎 入门教程一
- Photon服务器引擎入门
- Photon服务器引擎(一)Photon简介
- Photon服务器引擎(一)Photon简介
- Photon服务器入门二
- Unity Photon服务器(二)
- Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现
- Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现
- 开始整理几年技术生涯的经验和笔记
- C语言中一些关键字的作用
- Dynamic Class Loading in Java
- AJAX请求异常代码含义
- 使用vs2010编译CCV1.5
- Photon服务器引擎 入门教程二
- 使用并行计算大幅提升递归算法效率
- git-svn提交修改出错时的恢复办法
- JAVA经典算法40题(18)
- java实现sftp实例
- 嵌入式系统基础
- 黑马程序员 Java初级<二>--->数据、变量与运算
- log4j:ERROR Could not find value for key log4j.appender.CONSOLE
- JAVA 数组复制