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

相关文章

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

速了解MySQL 数据库不同存储引擎

快速了解MySQL 数据库不同存储引擎 MySQL 提供了多种存储引擎,每种存储引擎都有其特定的特性和适用场景。了解这些存储引擎的特性,有助于在设计数据库时做出合理的选择。以下是 MySQL 中几种常用存储引擎的详细介绍。 1. InnoDB 特点: 事务支持:InnoDB 是一个支持 ACID(原子性、一致性、隔离性、持久性)事务的存储引擎。行级锁:使用行级锁来提高并发性,减少锁竞争

速盾:直播 cdn 服务器带宽?

在当今数字化时代,直播已经成为了一种非常流行的娱乐和商业活动形式。为了确保直播的流畅性和高质量,直播平台通常会使用 CDN(Content Delivery Network,内容分发网络)服务器来分发直播流。而 CDN 服务器的带宽则是影响直播质量的一个重要因素。下面我们就来探讨一下速盾视角下的直播 CDN 服务器带宽问题。 一、直播对带宽的需求 高清视频流 直播通常需要传输高清视频

一种改进的red5集群方案的应用、基于Red5服务器集群负载均衡调度算法研究

转自: 一种改进的red5集群方案的应用: http://wenku.baidu.com/link?url=jYQ1wNwHVBqJ-5XCYq0PRligp6Y5q6BYXyISUsF56My8DP8dc9CZ4pZvpPz1abxJn8fojMrL0IyfmMHStpvkotqC1RWlRMGnzVL1X4IPOa_  基于Red5服务器集群负载均衡调度算法研究 http://ww

RTMP流媒体服务器 crtmpserver

http://www.oschina.net/p/crtmpserver crtmpserver又称rtmpd是Evostream Media Server(www.evostream.com)的社区版本采用GPLV3授权 其主要作用为一个高性能的RTMP流媒体服务器,可以实现直播与点播功能多终端支持功能,在特定情况下是FMS的良好替代品。 支持RTMP的一堆协议(RT

Smarty模板引擎工作机制(一)

深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解。 在没有使用Smarty模板引擎的情况下,我们都是将PHP程序和网页模板合在一起编辑的,好比下面的源代码: <?php$title="深处浅出之Smarty模板引擎工作机制";$content=

云原生之高性能web服务器学习(持续更新中)

高性能web服务器 1 Web服务器的基础介绍1.1 Web服务介绍1.1.1 Apache介绍1.1.2 Nginx-高性能的 Web 服务端 2 Nginx架构与安装2.1 Nginx概述2.1.1 Nginx 功能介绍2.1.2 基础特性2.1.3 Web 服务相关的功能 2.2 Nginx 架构和进程2.2.1 架构2.2.2 Ngnix进程结构 2.3 Nginx 模块介绍2.4

Weex入门教程之4,获取当前全局环境变量和配置信息(屏幕高度、宽度等)

$getConfig() 获取当前全局环境变量和配置信息。 Returns: config (object): 配置对象;bundleUrl (string): bundle 的 url;debug (boolean): 是否是调试模式;env (object): 环境对象; weexVersion (string): Weex sdk 版本;appName (string): 应用名字;

Weex入门教程之3,使用 Vue 开发 Weex 页面

环境安装 在这里简略地介绍下,详细看官方教程 Node.js 环境 Node.js官网 通常,安装了 Node.js 环境,npm 包管理工具也随之安装了。因此,直接使用 npm 来安装 weex-toolkit。 npm 是一个 JavaScript 包管理工具,它可以让开发者轻松共享和重用代码。Weex 很多依赖来自社区,同样,Weex 也将很多工具发布到社区方便开发者使用。