PhotonServer游戏服务器端(二)
来源:互联网 发布:单片机简易电子琴c语言 编辑:程序博客网 时间:2024/05/29 11:57
前面已经把服务器搭建好了, 现在来整理客户端
1,打开unity3D新建工程PhotonServerUnityClient,新建文件夹Plugins 打开服务器目录找到lib找到Photon3Unity3D.dll 导入Plugins ,然后新建空节点命名为PhotonEngine添加PhotonEngine脚本 实现内容入下:
using System.Collections;using System.Collections.Generic;using UnityEngine;using ExitGames.Client.Photon;using System;public class PhotoEngine : MonoBehaviour,IPhotonPeerListener { private static PhotoEngine _instance; // 全局单例模式 private PhotonPeer peer; public void DebugReturn(DebugLevel level, string message) { throw new NotImplementedException(); } //服务器端向客户端发起数据的时候 public void OnEvent(EventData eventData) { throw new NotImplementedException(); } //客户端向服务器发起一个请求以后服务器处理完以后 就会给客户端一个相应 public void OnOperationResponse(OperationResponse operationResponse) { throw new NotImplementedException(); } //状态改变的时候调用该方法 PeerStateValue.。。。 public void OnStatusChanged(StatusCode statusCode) { Debug.Log(statusCode); } void Awake() { if (_instance==null) { _instance = this; DontDestroyOnLoad(this.gameObject); } else if (_instance!=this) { //所有场景只留一个PhotonEngine 删除多余的 Destroy(this.gameObject);return; } }void Start() { //通过listener来接收服务器端的相应 peer = new PhotonPeer(this,ConnectionProtocol.Udp); //连接服务器 发起连接 peer.Connect("127.0.0.1:5055", "MyGame1"); }void Update () { peer.Service(); //发起请求 无论什么状态下 } void OnDestroy() { if (peer!=null&&(peer.PeerState==PeerStateValue.Connected)) // 当peer不等于空 而且 正在运行的时候 { peer.Disconnect();//断开连接 } }}
2,客服端和服务器进行参数交互 原理如图所示
客户端新建一个Test脚本挂在PhotonEngine用来跟服务器发送请求 代码实现如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test : MonoBehaviour {// Update is called once per framevoid Update () { if (Input.GetMouseButtonDown(0)) { SendRequest(); }} void SendRequest() { Dictionary<byte, object> data = new Dictionary<byte, object>(); data.Add(1, 100); data.Add(2, "nihaoma"); PhotoEngine.Peer.OpCustom(1, data, true); //向服务器发起请求⑴ }}
服务器那边接收到客户端的参数后 回应客户端的请求 代码实现如下 :
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Photon.SocketServer;using PhotonHostRuntimeInterfaces;namespace MyGameServer{ public class ClientPeer : Photon.SocketServer.ClientPeer // { public ClientPeer(InitRequest initRequest) : base(initRequest) { } //断开连接 清理工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { } //处理客户端发起请求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { switch (operationRequest.OperationCode)//通过OPCode区分请求 { case 1 : MyGameServer.log.Info("收到了一个客户端的请求"); Dictionary<byte,object> data= operationRequest.Parameters; //服务器接到客户端的请求⑴ object intvalue; data.TryGetValue(1, out intvalue); object stringValue; data.TryGetValue(2, out stringValue); MyGameServer.log.Info("得到的参数数据是"+" " + intvalue.ToString() + " " + stringValue.ToString()); //-------------------------------------------------------------- Dictionary<byte, object> data2 = new Dictionary<byte, object>(); data2.Add(1, 10220); data2.Add(2, "niha22oma"); OperationResponse opResponse = new OperationResponse(1, data2); //服务器向客户端回应请求 //opResponse.SetParameters(data2); //这个方法集成在 new OperationResponse(1, data2)了 SendOperationResponse(opResponse, sendParameters); //回传给客户端 给客户端一个回应 break; case 2: break; default: break; } } }}
客户端接收到服务器的回应 代码实现如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;using ExitGames.Client.Photon; //插件的命名空间using System;public class PhotoEngine : MonoBehaviour,IPhotonPeerListener { public static PhotonPeer Peer { get { return peer; } } private static PhotoEngine _instance; // 全局单例模式 private static PhotonPeer peer; public void DebugReturn(DebugLevel level, string message) { throw new NotImplementedException(); } //服务器端向客户端发起数据的时候 public void OnEvent(EventData eventData) { throw new NotImplementedException(); } //客户端向服务器发起一个请求以后服务器处理完以后 就会给客户端一个相应 public void OnOperationResponse(OperationResponse operationResponse) { switch (operationResponse.OperationCode) { case 1: Debug.Log("收到了服务器的相应OpColde: 1"); Dictionary<byte, object> data2 = operationResponse.Parameters; object intvalue; data2.TryGetValue(1, out intvalue); object stringValue; data2.TryGetValue(2, out stringValue); Debug.Log(intvalue.ToString() + " " + stringValue.ToString()); break; case 2: break; default: break; } } //状态改变的时候调用该方法 PeerStateValue.。。。 public void OnStatusChanged(StatusCode statusCode) { Debug.Log(statusCode); } void Awake() { if (_instance==null) { _instance = this; DontDestroyOnLoad(this.gameObject); } else if (_instance!=this) { //所有场景只留一个PhotonEngine 删除多余的 Destroy(this.gameObject);return; } }void Start () { //通过listener来接收服务器端的相应 peer = new PhotonPeer(this,ConnectionProtocol.Udp); //连接服务器 发起连接 peer.Connect("127.0.0.1:5055", "MyGame1"); }void Update () { peer.Service(); //检测有没有 发起请求 无论什么状态下 //peer.Service(); } void OnDestroy() { if (peer!=null&&(peer.PeerState==PeerStateValue.Connected)) // 当peer不等于空 而且 正在运行的时候 { peer.Disconnect();//断开连接 } }}
3,接下来是 如果客户端不像服务器发送请求 服务器是没办法通过 SendOperationResponse 像数据发送请求,如果服务器想想客户端发送请求该怎么办呢 代码实现如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Photon.SocketServer;using PhotonHostRuntimeInterfaces;namespace MyGameServer{ public class ClientPeer : Photon.SocketServer.ClientPeer // { public ClientPeer(InitRequest initRequest) : base(initRequest) { } //断开连接 清理工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { } //处理客户端发起请求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { switch (operationRequest.OperationCode)//通过OPCode区分请求 { case 1: MyGameServer.log.Info("收到了一个客户端的请求"); Dictionary<byte, object> data = operationRequest.Parameters; //服务器接到客户端的请求⑴ object intvalue; data.TryGetValue(1, out intvalue); object stringValue; data.TryGetValue(2, out stringValue); MyGameServer.log.Info("得到的参数数据是" + " " + intvalue.ToString() + " " + stringValue.ToString()); //-------------------------------------------------------------- Dictionary<byte, object> data2 = new Dictionary<byte, object>(); data2.Add(1, 10220); data2.Add(2, "niha22oma"); OperationResponse opResponse = new OperationResponse(1, data2); //服务器向客户端回应请求 //opResponse.SetParameters(data2); //这个方法集成在 new OperationResponse(1, data2)了 SendOperationResponse(opResponse, sendParameters); //回传给客户端 给客户端一个相应 //===========如果客户端没有想服务器发送请求 服务器要通知客户端的情况下要用下面这种方法================= EventData eventdata = new EventData(1);//也可以写成 EventData eventdata = new EventData(1,data2); 当然下面那一步就不要了 eventdata.Parameters = data2; SendEvent(eventdata, sendParameters); //sendParameters 或者写成new SendParameters();是不影响的 ----在服务器的任何地方都可以调用 break; case 2: break; default: break; } } }}然后在客户端 接收到 服务器发送的事件 并进行解析代码实现如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;using ExitGames.Client.Photon; //插件的命名空间using System;public class PhotoEngine : MonoBehaviour,IPhotonPeerListener { public static PhotonPeer Peer { get { return peer; } } private static PhotoEngine _instance; // 全局单例模式 private static PhotonPeer peer; public void DebugReturn(DebugLevel level, string message) { throw new NotImplementedException(); } //服务器端向客户端发起数据的时候 public void OnEvent(EventData eventData) {//这个方法会接收所有服务器发来的事件 所以要用switch判断一下code switch (eventData.Code) { case 1: Debug.Log("收到服务器发过来的事件 "); Dictionary <byte,object> data3= eventData.Parameters; object intvalue; data3.TryGetValue(1, out intvalue); object stringValue; data3.TryGetValue(2, out stringValue); Debug.Log(intvalue.ToString() + " " + stringValue.ToString()); break; default: break; } } //客户端向服务器发起一个请求以后服务器处理完以后 就会给客户端一个相应 public void OnOperationResponse(OperationResponse operationResponse) { switch (operationResponse.OperationCode) { case 1: Debug.Log("收到了服务器的相应OpColde: 1"); Dictionary<byte, object> data2 = operationResponse.Parameters; object intvalue; data2.TryGetValue(1, out intvalue); object stringValue; data2.TryGetValue(2, out stringValue); Debug.Log(intvalue.ToString() + " " + stringValue.ToString()); break; case 2: break; default: break; } } //状态改变的时候调用该方法 PeerStateValue.。。。 public void OnStatusChanged(StatusCode statusCode) { Debug.Log(statusCode); } void Awake() { if (_instance==null) { _instance = this; DontDestroyOnLoad(this.gameObject); } else if (_instance!=this) { //所有场景只留一个PhotonEngine 删除多余的 Destroy(this.gameObject);return; } }void Start () { //通过listener来接收服务器端的相应 peer = new PhotonPeer(this,ConnectionProtocol.Udp); //连接服务器 发起连接 peer.Connect("127.0.0.1:5055", "MyGame1"); }void Update () { peer.Service(); //检测有没有 发起请求 无论什么状态下 //peer.Service(); } void OnDestroy() { if (peer!=null&&(peer.PeerState==PeerStateValue.Connected)) // 当peer不等于空 而且 正在运行的时候 { peer.Disconnect();//断开连接 } }}
阅读全文
0 0
- PhotonServer游戏服务器端(二)
- PhotonServer游戏服务器端(一)
- PhotonServer游戏服务器端(三)
- PhotonServer游戏服务器端(四)
- PhotonServer-MyGameServer服务器端
- PhotonServer MMO游戏开发
- PhotonServer游戏服务器
- PhotonServer游戏服务器【完整版】
- 使用PhotonServer和Unity建立一个棋牌类游戏实例(二)构建photon服务器与数据库的交互
- 自己做游戏(一)-PhotonServer配置
- PhotonServer游戏服务器部署及日志输出
- 网络在线游戏开发心得(服务器端)(二)
- PhotonServer学习笔记(一)
- 使用PhotonServer和Unity建立一个棋牌类游戏实例(一)构建photon服务器基础架构
- H5游戏开发的架构总结(二) 服务器端
- 学习UNITY3D网络插件photon pun和PhotonServer<二>
- MMORPG游戏服务器端设计(续)
- 经典游戏服务器端架构(一)
- 常见sql技巧 优化
- 算法学习笔记5-递归方法写一个全排列
- jzoj5290 【NOIP2017提高组A组模拟8.17】行程的交集 (树上路径交,dfs序+树状数组维护姿势)
- android 随机生成验证码
- http请求包和响应包介绍
- PhotonServer游戏服务器端(二)
- git 一、基本配置
- 2017腾讯校招编程
- 谷歌页面等调试时总是报No Found 404 错误
- Git 设置公密钥登录方式
- 软件开发之持续改进
- leecode 73Set Matrix Zeroes
- instanceof, isinstance,isAssignableFrom的区别
- 简单LinuxC程序关于任意长整数相加(字符串实现)