上一讲中主要介绍了服务器的简单知识,配置服务器和客户端连接.
第二讲介绍客户端请求服务器,服务器响应操作,我们就以一个简单的用户登录为基础介绍吧
一、服务器端
按照上一篇教程我们配置好简单的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 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
}
项目源码免积分下载:服务器端和客户端