Unity3d客户端与PhotonServer通信
来源:互联网 发布:四海认证淘宝渔具钓竿 编辑:程序博客网 时间:2024/05/16 19:06
先打开Unity3d,在Assert下创建一个Plugins文件夹,将PhotonServer SDK文件夹下lib下的Photon3Unity3D.dll拖到Plugins文件夹下
服务器端和客户端之间的关系图:
创建一个空物体,名字为PhotonEngine,创建一个脚本,名字为PhotonEngine:
using ExitGames.Client.Photon;using System.Collections;using System.Collections.Generic;using UnityEngine;public class PhotonEngine : MonoBehaviour,IPhotonPeerListener { private static PhotonEngine Instance; private static PhotonPeer peer; public static PhotonPeer Peer { get { return peer; } } void Awake() { if(Instance==null) //单例模式的处理,只有一个Instance { Instance = this; DontDestroyOnLoad(this.gameObject); //不销毁这个物体 } else if(Instance!=this) { Destroy(this.gameObject);return; //删除多余的PhotonEngine } }// Use this for initializationvoid Start () { //通过Listender接收服务器端的响应 peer = new PhotonPeer(this, ConnectionProtocol.Udp); peer.Connect("127.0.0.1:5055", "MyGame1"); //连接服务器端}// Update is called once per framevoid Update () { //if(peer.PeerState==PeerStateValue.Connected) //判断peer已经连接 //{ peer.Service(); //什么状态下都要执行 //} } void OnDestroy() { if(peer!=null && peer.PeerState==PeerStateValue.Connecting) { peer.Disconnect(); //断开连接 } } public void DebugReturn(DebugLevel level, string message) { } //处理服务器端直接发送过来的信息 public void OnEvent(EventData eventData) { switch(eventData.Code) { case 1: Dictionary<byte,object> data = eventData.Parameters; object intValue; data.TryGetValue(1, out intValue); object stringValue; data.TryGetValue(2, out stringValue); Debug.Log("接收服务器直接发送来的信息:"+intValue.ToString()+stringValue.ToString()); break; case 2: break; default: break; } } //接收服务器发来的响应 public void OnOperationResponse(OperationResponse operationResponse) { switch(operationResponse.OperationCode) { case 1: Debug.Log("接收一个来自服务器的响应"); Dictionary<byte,object> data = operationResponse.Parameters; object intValue; data.TryGetValue(1, out intValue); object stringValue; data.TryGetValue(2, out stringValue); Debug.Log("接收来自服务器端的响应数据:" + intValue.ToString() + stringValue.ToString()); break; case 2: break; default: break; } } public void OnStatusChanged(StatusCode statusCode) { Debug.Log(statusCode); }}
创建一个Test脚本,放在PhotonEngine物件上:
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>(); int intValue=100; //向服务器端发送数据 data.Add(1, intValue); string stringValue = "hfasfadh滑石粉哈"; data.Add(2, stringValue); PhotonEngine.Peer.OpCustom(1, data, true); //向服务器端发送信息 }}
在服务器端的ClientPeer类中实现通信:
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()); OperationResponse opResponse = new OperationResponse(1); Dictionary<byte, object> data2 = new Dictionary<byte, object>(); int intValue2 = 100; string stringValue2 = "asfaasdf发了疯has"; data2.Add(1, intValue2); data2.Add(2, stringValue2); opResponse.SetParameters(data2); //向客户端回应数据 SendOperationResponse(opResponse, sendParameters); //向客户端发出响应 EventData ed = new EventData(1); Dictionary<byte, object> data3 = new Dictionary<byte, object>(); int intValue3 = 200; data3.Add(1, intValue3); string stringValue3 = "fdasf飞杰"; data3.Add(2, stringValue3); ed.SetParameters(data3); SendEvent(ed, new SendParameters()); //直接向客户端发送信息 break; case 2: break; default: break; } } }}
阅读全文
0 0
- Unity3d客户端与PhotonServer通信
- unity3d Android客户端与PC服务器实现Socket通信(wifi)
- unity3d Android客户端与PC服务器实现Socket通信(wifi)
- unity3d Android客户端与PC服务器实现Socket通信(wifi)
- Unity3d客户端保存cookie与java服务器通信
- unity3d Android客户端与PC服务器实现Socket通信(wifi)
- Unity3d通过PhotonServer访问MySQL数据库
- 客户端与服务器通信
- 服务器与客户端通信
- 客户端与服务器端通信
- 客户端与服务器通信
- unity3d socket( unity socket )客户端通信插件-在Unity3d中的简易实现网游客户端通信框架
- [Unity3d]unity与html通信
- Unity3D中与Android通信
- unity3D与ios互相通信
- unity3D与Socket通信_01
- 学习UNITY3D网络插件photon pun和PhotonServer<一>
- 学习UNITY3D网络插件photon pun和PhotonServer<二>
- Django 创建/删除用户
- 动态规划-最短路径个数问题
- 企业开发中选择logback而不是log4j的理由
- BZOJ
- tomcat常见错误跳转到指定页面
- Unity3d客户端与PhotonServer通信
- C/C++学习之C提高-----结构体定义、初始化、引用、结构体做函数参数、结构体数组、结构体中套一级/二级指针、结构体深copy和浅copy
- 自定义键盘
- Linux共享内存(顺便求解惑)
- html中meta标签详解
- 关于str函数的使用
- JavaScript实现一个简单数据结构树
- Maven中的settings.xml设置远程镜像库
- 小白成长之路(7)--C++多层派生类继承实例(期中考试)