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-10808deployMyChartServerbin
配置文件
ExitGames-Photon-Server-SDK_v3-4-31-10808deploybin_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; } } }
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:www.shinetop.cn