unity服务器Photon Server学习笔记
来源:互联网 发布:mac 搜狗输入法切换 编辑:程序博客网 时间:2024/05/29 03:33
(一)服务端
新建类库
引用:ExitGamesLibs,Photon.SocketServer,PhotonHostRuntimeInterfaces
ApplicationBase
using chatServer.Properties;using Photon.SocketServer;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace chatServer{ /// <summary> /// 继承applicationBase的类是入口程序,也是启动程序 /// </summary> public class ChartServer : ApplicationBase { /// <summary> /// 客户端连接到这个Server端调用 /// </summary> /// <param name="initRequest"></param> /// <returns></returns> protected override PeerBase CreatePeer(InitRequest initRequest) { return new ChartPeerBase(initRequest.Protocol, initRequest.PhotonPeer); } /// <summary> /// Server端启动时调用 /// </summary> protected override void Setup() { } /// <summary> /// 这个Server端停掉时调用 /// </summary> protected override void TearDown() { } }}
PeerBase
using Photon.SocketServer;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using PhotonHostRuntimeInterfaces;namespace chatServer.Properties{ /// <summary> /// 用来和客户端进行通信 /// </summary> class ChartPeerBase : PeerBase { public ChartPeerBase(IRpcProtocol protocol, IPhotonPeer unmanagedPeer):base(protocol,unmanagedPeer) { } protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { throw new NotImplementedException(); } protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { throw new NotImplementedException(); } }}
设置输出
在下载好的sdk目录下新建文件MyChartServer,并在文件下新建bin目录
项目-chatserver属性(Alt+F7)-生成-输出路径为ExitGames-Photon-Server-SDK_v3-4-31-10808\deploy\MyChartServer\bin\
配置文件
ExitGames-Photon-Server-SDK_v3-4-31-10808\deploy\bin_Win64\配置PhotonServer.config文件下
Application Name=”Lite”和”LiteLobby”间加入新的Application,配置如下
<Applications Default="Lite"> <!-- Lite Application --> <Application Name="Lite" BaseDirectory="Lite" Assembly="Lite" Type="Lite.LiteApplication" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application> <!-- ChartServer Application --> <Application Name="ChartServer"//自定义Name BaseDirectory="MyChartServer"//文件目录,如我的文件在deploy下MyChartServer文件夹下 Assembly="chatServer"//继承ApplicationBase的类所在命名空间 Type="chatServer.ChartServer"//继承ApplicationBase的类所在命名空间+类名 ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application> <!-- LiteLobby Application --> <Application Name="LiteLobby" BaseDirectory="LiteLobby" Assembly="LiteLobby" Type="LiteLobby.LiteLobbyApplication" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application>
(二)客户端
新建控制台程序
引用 Photon3DotNet
using ExitGames.Client.Photon;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace PhotonChatServerClient{ class ChatServerListener : IPhotonPeerListener { public bool isconnect = false; public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { } public void OnStatusChanged(StatusCode statusCode) { switch (statusCode) { case StatusCode.Connect: isconnect = true; Console.WriteLine("Connect"); break; } } } class Program { static void Main(string[] args) { ChatServerListener listener=new ChatServerListener(); PhotonPeer peer=new PhotonPeer(listener,ConnectionProtocol.Tcp);//第二个参数为选择的协议 peer.Connect("127.0.0.1:4530", "ChartServer");//连接服务器,4530为config文件指定协议所对应的端口号,Chartserver为之前的服务端应用 while (!listener.isconnect)//判断是否建立连接 { peer.Service();//这个调用完才能向服务器发起请求 } } }}
(三)运行客户端服务端,完成连接
(四)客户端发起请求
修改之前的客户端
using ExitGames.Client.Photon;using System;using System.Collections.Generic;using System.Data.SqlTypes;using System.Linq;using System.Text;using System.Threading.Tasks;namespace PhotonChatServerClient{ class ChatServerListener : IPhotonPeerListener { public bool isconnect = false; public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { } public void OnStatusChanged(StatusCode statusCode) { switch (statusCode) { case StatusCode.Connect: isconnect = true; Console.WriteLine("Connect"); break; } } } class Program { static void Main(string[] args) { ChatServerListener listener=new ChatServerListener(); PhotonPeer peer=new PhotonPeer(listener,ConnectionProtocol.Tcp); peer.Connect("127.0.0.1:4530", "ChartServer");//连接服务器 while (!listener.isconnect) { peer.Service(); } Dictionary<byte,object> dict=new Dictionary<byte, object>(); dict.Add(1,"username"); dict.Add(2,"password"); peer.OpCustom(1, dict, true); while (true) { //死循环防止程序终止 peer.Service();//发出去 } } }}
(五)服务端响应
修改之前的服务端PeerBase
/// 当客户端发起请求时调用 /// </summary> /// <param name="operationRequest"></param> /// <param name="sendParameters"></param> protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { Dictionary<byte, object> dict = new Dictionary<byte, object>(); dict.Add(1,"siki"); OperationResponse response=new OperationResponse(1,dict); SendOperationResponse(response, sendParameters); } }
(六)客户端获得服务端的响应
/// <summary> /// 得到服务器端的响应 /// </summary> /// <param name="operationResponse"></param> public void OnOperationResponse(OperationResponse operationResponse) { Dictionary<byte, object> dict = operationResponse.Parameters; object val = null; dict.TryGetValue(1,out val); Console.WriteLine("getserver"+val.ToString()); }
(七)unity客户端
目录Plugin下引入sdk/lib/Photon3Unity3D.dll
using System.Collections;using System.Collections.Generic;using System.Runtime.CompilerServices;using ExitGames.Client.Photon;using UnityEngine;public class PhotonServerEngine : MonoBehaviour ,IPhotonPeerListener{ private PhotonPeer peer; private bool isconnect = false; void Start() { peer = new PhotonPeer(this, ConnectionProtocol.Tcp); peer.Connect("127.0.0.1:4530", "ChartServer");//连接服务器 } void Update() { peer.Service(); } void OnGUI() { if (isconnect) { if (GUILayout.Button("Send")) { Dictionary<byte, object> dict = new Dictionary<byte, object>(); dict.Add(1, "username"); dict.Add(2, "password"); peer.OpCustom(1, dict, true); } } } public void DebugReturn(DebugLevel level, string message) { Debug.Log(level+":"+message); } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { Dictionary<byte, object> dict = operationResponse.Parameters; object val = null; dict.TryGetValue(1, out val); Debug.Log("getserver" + val.ToString()); } public void OnStatusChanged(StatusCode statusCode) { switch (statusCode) { case StatusCode.Connect: isconnect = true; Debug.Log("Connect"); break; } }}
阅读全文
1 0
- unity服务器Photon Server学习笔记
- Unity链接Photon服务器
- Unity Photon服务器
- Unity Photon服务器(二)
- unity基础开发-photon服务器
- Unity环境搭建Photon服务器
- 新手学习Photon之创建Photon服务器
- Photon学习笔记
- photon mapping学习笔记
- Photon学习笔记(三)
- photon mapping学习笔记
- Photon Server与unity客户端连接
- Unity连Photon服务器入门详解
- Photon Server游戏服务器从零开始学习(一)部署第一个服务器程序
- Photon学习笔记(一)
- Photon学习笔记(二)
- Photon学习笔记(一)
- Photon学习笔记(二)
- 求这个字符串中的最大的数字字符串
- java之正则表达式
- Java中hashcode
- 关于UCP和TCP的形象理解
- 秋招来袭
- unity服务器Photon Server学习笔记
- laravel 5.2队列延时
- 一个有趣的心理测试
- 如何在页面引用外部的HTML?
- bzoj3160 万径人踪灭
- ActiveMQ消息队列之java消息生产与消费
- 2017年年中总结——爱在继续……
- JS中隐藏的坑
- QML之TabView