unity基础开发----Photon服务器引擎 入门教程二
来源:互联网 发布:什么图案画数据对比表 编辑:程序博客网 时间:2024/05/22 03:39
上一讲中主要介绍了服务器的简单知识,配置服务器和客户端连接.
第二讲介绍客户端请求服务器,服务器响应操作,我们就以一个简单的用户登录为基础介绍吧
一、服务器端
按照上一篇教程我们配置好简单的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
- unity基础开发----Photon服务器引擎 入门教程二
- unity基础开发----Photon服务器引擎 入门教程一
- Photon服务器引擎 入门教程二
- Photon服务器引擎 入门教程二
- Photon服务器引擎 入门教程二
- unity基础开发-photon服务器
- Photon服务器引擎 入门教程
- Photon服务器入门教程二
- Photon服务器引擎 入门教程一
- Photon服务器引擎 入门教程一
- Photon服务器引擎 入门教程一
- Photon服务器引擎入门教程一
- Unity Photon服务器(二)
- Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现
- Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现
- Photon服务器引擎入门
- Unity链接Photon服务器
- Unity Photon服务器
- unity基础开发----Photon服务器引擎 入门教程一
- 如何在事业上获得成功?
- Winxp下安装Git,并从GitHub下载第一个工程MyBoo
- 你必须知道的 34 个简单实用的 Ubuntu 快捷键
- java与设计模式之观察者模式
- unity基础开发----Photon服务器引擎 入门教程二
- poj1179 Polygon
- HTTP协议 (三) 压缩
- 数字图像处理与机器视觉张铮版[code]
- 看到一篇仓央嘉措诗歌集的相关日志,追溯了里面的诗歌来源
- .net 2007数据库性能分析(分页)
- Oracel 编写控制结构
- 编译预处理
- 【rmzt】XP系统动漫桌面主题下载