unity服务器PhotonServer学习笔记
(一)服务端
新建类库
引用: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 { ///
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 { ///
设置输出
在下载好的sdk目录下新建文件MyChartServer,并在文件下新建bin目录
项目-chatserver属性(Alt+F7)-生成-输出路径为ExitGames-Photon-Server-SDK_v3-4-31-10808deployMyChartServerbin
配置文件
ExitGames-Photon-Server-SDK_v3-4-31-10808deploybin_Win64配置PhotonServer.config文件下
Application Name=”Lite”和”LiteLobby”间加入新的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
(五)服务端响应
修改之前的服务端PeerBase
/// 当客户端发起请求时调用 /// /// /// protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { Dictionary
(六)客户端获得服务端的响应
///
(七)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
标题名称:unity服务器PhotonServer学习笔记
文章链接:http://hbruida.cn/article/chdpoi.html