Photon服务器入门二

上一讲中主要介绍了服务器的简单知识,配置服务器和客户端连接.

第二讲介绍客户端请求服务器,服务器响应操作,我们就以一个简单的用户登录为基础介绍吧

一、服务器端

按照上一篇教程我们配置好简单的photon服务器,但是只能用于连接服务器和断开服务器操作,其他的基本没有提到,今天是要在上一讲基础上添加内容.

主要是在MyPeer.cs类的OnOperationRequest方法中实现,代码如下:

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using Photon.SocketServer;  
  4. using PhotonHostRuntimeInterfaces;  
  5.   
  6.   
  7. namespace MyServer  
  8. {  
  9.     using Message;  
  10.     using System.Collections;  
  11.   
  12.     public class MyPeer : PeerBase  
  13.     {  
  14.         Hashtable userTable;  
  15.   
  16.   
  17.         public  MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)  
  18.             : base(protocol, photonPeer)  
  19.         {  
  20.             userTable = new Hashtable();  
  21.             userTable.Add("user1""pwd1");  
  22.             userTable.Add("user2""pwd2");  
  23.             userTable.Add("user3""pwd3");  
  24.             userTable.Add("user4""pwd4");  
  25.             userTable.Add("user5""pwd5");  
  26.         }  
  27.   
  28.         protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)  
  29.         {  
  30.               
  31.         }  
  32.   
  33.         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)  
  34.         {  
  35.             switch (operationRequest.OperationCode) {   
  36.                 case (byte)OpCodeEnum.Login:  
  37.                     string uname = (string)operationRequest.Parameters[(byte)OpKeyEnum.UserName];  
  38.                     string pwd = (string)operationRequest.Parameters[(byte)OpKeyEnum.PassWord];  
  39.   
  40.                     if (userTable.ContainsKey(uname) && userTable[uname].Equals(pwd))//login success  
  41.                     {  
  42.                         SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginSuccess, null),new SendParameters());  
  43.                     }  
  44.                     else  
  45.                     { //login fauled  
  46.                         SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginFailed, null), new SendParameters());  
  47.                     }  
  48.                     break;  
  49.             }  
  50.         }  
  51.     }  
  52. }  
OnOperationRequest方法中验证用户名和密码,然后发送响应给客户端.需要用到的枚举一个类如下:

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace MyServer.Message  
  7. {  
  8.     enum OpCodeEnum : byte  
  9.     {  
  10.         //login  
  11.         Login = 249,  
  12.         LoginSuccess = 248,  
  13.         LoginFailed = 247,  
  14.   
  15.         //room  
  16.         Create = 250,  
  17.         Join = 255,  
  18.         Leave = 254,  
  19.         RaiseEvent = 253,  
  20.         SetProperties = 252,  
  21.         GetProperties = 251  
  22.     }  
  23.   
  24.   
  25.     enum OpKeyEnum : byte  
  26.     {  
  27.         RoomId = 251,  
  28.         UserName = 252,  
  29.         PassWord = 253  
  30.     }  
  31. }  

二、客户端

客户端过程需要请求服务器并接收服务器的响应下面上代码,就一个类搞定:

[csharp]  view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. using ExitGames.Client.Photon;  
  6.   
  7.   
  8.   
  9.   
  10.   
  11. public class TestConnection : MonoBehaviour,IPhotonPeerListener {  
  12.     public string address;  
  13.       
  14.     PhotonPeer peer;  
  15.     ClientState state = ClientState.DisConnect;  
  16.       
  17.     string username = "";  
  18.     string password = "";  
  19.     // Use this for initialization  
  20.     void Start () {  
  21.         peer = new PhotonPeer(this,ConnectionProtocol.Udp);  
  22.     }  
  23.       
  24.     // Update is called once per frame  
  25.     void Update () {  
  26.         peer.Service();  
  27.     }  
  28.       
  29.     public GUISkin skin;  
  30.     void OnGUI(){  
  31.         GUI.skin = skin;  
  32.           
  33.         switch(state){  
  34.         case ClientState.DisConnect:  
  35.             GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"click the button to connect.");  
  36.             if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,100,30),"Connect")){  
  37.                 peer.Connect(address,"MyServer");  
  38.                 state = ClientState.Connecting;  
  39.             }  
  40.             break;  
  41.         case ClientState.Connecting:  
  42.             GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connecting to Server...");  
  43.             break;  
  44.         case ClientState.Connected:  
  45.             GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));  
  46.               
  47.             //  
  48.             GUILayout.BeginVertical();  
  49.               
  50.             GUILayout.Label("Connect Success! Please Login.");  
  51.             //draw username  
  52.             GUILayout.BeginHorizontal();  
  53.             GUILayout.Label("UserName:");  
  54.             username = GUILayout.TextField(username);  
  55.             GUILayout.EndVertical();  
  56.               
  57.             //draw password  
  58.             GUILayout.BeginHorizontal();  
  59.             GUILayout.Label("Password:");  
  60.             password = GUILayout.TextField(password);  
  61.             GUILayout.EndVertical();  
  62.               
  63.             //draw buttons  
  64.             GUILayout.BeginHorizontal();  
  65.             if(GUILayout.Button("login")){  
  66.                 userLogin(username,password);  
  67.             }  
  68.               
  69.               
  70.             if(GUILayout.Button("canel")){  
  71.                 state = ClientState.DisConnect;  
  72.             }  
  73.             GUILayout.EndVertical();  
  74.               
  75.             GUILayout.EndVertical();  
  76.             GUILayout.EndArea();  
  77.               
  78.             break;  
  79.         case ClientState.ConnectFailed:  
  80.             GUI.Label(new Rect(Screen.width/2,Screen.height/2-30,300,40),"Connect Failed.");  
  81.               
  82.             break;  
  83.         case ClientState.LoginSuccess:  
  84.             GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));  
  85.             GUILayout.Label("Login Success!");  
  86.             GUILayout.EndArea();  
  87.             break;  
  88.         case ClientState.LoginFailed:  
  89.             GUILayout.BeginArea(new Rect((Screen.width-500)/2,(Screen.height-400)/2,500,400));  
  90.             GUILayout.Label("Login Failed!");  
  91.             GUILayout.EndArea();  
  92.             break;  
  93.         }  
  94.     }  
  95.      
  96.     #region My Method  
  97.     IEnumerator connectFailedHandle(){  
  98.         yield return new WaitForSeconds(1);  
  99.         state = ClientState.DisConnect;  
  100.     }  
  101.       
  102.     void userLogin(string uname,string pwd){  
  103.         Debug.Log("userLogin");  
  104.         Dictionary<byte,object> param = new Dictionary<byteobject>();  
  105.         param.Add((byte)OpKeyEnum.UserName,uname);  
  106.         param.Add((byte)OpKeyEnum.PassWord,pwd);  
  107.         peer.OpCustom((byte)OpCodeEnum.Login,param,true);  
  108.     }  
  109.       
  110.     IEnumerator loginFailedHandle(){  
  111.         yield return new WaitForSeconds(1);  
  112.         Debug.Log("loginFailedHandle");  
  113.         state = ClientState.Connected;  
  114.     }  
  115.     #endregion  
  116.      
  117.      
  118.      
  119.  
  120.     #region IPhotonPeerListener implementation  
  121.     public void DebugReturn (DebugLevel level, string message)  
  122.     {  
  123.           
  124.     }  
  125.   
  126.     public void OnOperationResponse (OperationResponse operationResponse)  
  127.     {  
  128.         switch(operationResponse.OperationCode)  
  129.         {  
  130.         case (byte)OpCodeEnum.LoginSuccess:  
  131.             Debug.Log("login success!");  
  132.             state = ClientState.LoginSuccess;  
  133.             break;  
  134.         case (byte)OpCodeEnum.LoginFailed:  
  135.             Debug.Log("login Failed!");  
  136.             state = ClientState.LoginFailed;  
  137.             StartCoroutine(loginFailedHandle());  
  138.             break;  
  139.         }  
  140.     }  
  141.   
  142.     public void OnStatusChanged (StatusCode statusCode)  
  143.     {  
  144.         switch(statusCode){  
  145.         case StatusCode.Connect:  
  146.             Debug.Log("Connect Success! Time:"+Time.time);  
  147.             state = ClientState.Connected;  
  148.             break;  
  149.         case StatusCode.Disconnect:  
  150.             state = ClientState.ConnectFailed;  
  151.             StartCoroutine(connectFailedHandle());  
  152.             Debug.Log("Disconnect! Time:"+Time.time);  
  153.             break;  
  154.         }  
  155.     }  
  156.   
  157.     public void OnEvent (EventData eventData)  
  158.     {  
  159.           
  160.     }  
  161.     #endregion  
  162. }  
  163.   
  164. public enum ClientState : byte{  
  165.     DisConnect,  
  166.     Connecting,  
  167.     Connected,  
  168.     ConnectFailed,  
  169.     LoginSuccess,  
  170.     LoginFailed  
  171. }  
  172.   
  173. public enum OpCodeEnum : byte  
  174. {  
  175.     //login  
  176.     Login = 249,  
  177.     LoginSuccess = 248,  
  178.     LoginFailed = 247,  
  179.   
  180.     //room  
  181.     Create = 250,  
  182.     Join = 255,  
  183.     Leave = 254,  
  184.     RaiseEvent = 253,  
  185.     SetProperties = 252,  
  186.     GetProperties = 251  
  187. }  
  188.   
  189.   
  190. public enum OpKeyEnum : byte  
  191. {  
  192.     RoomId = 251,  
  193.     UserName = 252,  
  194.     PassWord = 253  
  195. }  

项目源码免积分下载:服务器端客户端

















本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366469,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值