Photon服务器引擎 入门教程二

2024-04-17 14:18

本文主要是介绍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 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 fauledSendOperationResponse(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{//loginLogin = 249,LoginSuccess = 248,LoginFailed = 247,//roomCreate = 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
{//loginLogin = 249,LoginSuccess = 248,LoginFailed = 247,//roomCreate = 250,Join = 255,Leave = 254,RaiseEvent = 253,SetProperties = 252,GetProperties = 251
}public enum OpKeyEnum : byte
{RoomId = 251,UserName = 252,PassWord = 253
}

这篇关于Photon服务器引擎 入门教程二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/912027

相关文章

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然