{"id":5060,"date":"2024-01-13T19:29:52","date_gmt":"2024-01-13T11:29:52","guid":{"rendered":"https:\/\/www.kenpai.com\/cms\/5060\/"},"modified":"2024-01-13T19:29:52","modified_gmt":"2024-01-13T11:29:52","slug":"unity%e6%9c%8d%e5%8a%a1%e5%99%a8photon-server%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/www.kenpai.com\/cms\/5060\/","title":{"rendered":"unity\u670d\u52a1\u5668Photon Server\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<\/p>\n<p><strong>\uff08\u4e00\uff09\u670d\u52a1\u7aef<\/strong> <br \/><strong>\u65b0\u5efa\u7c7b\u5e93<\/strong> <br \/><strong>\u5f15\u7528<\/strong>\uff1aExitGamesLibs,Photon.SocketServer,PhotonHostRuntimeInterfaces<\/p>\n<p style=\"text-align:\">\n<p><strong>ApplicationBase<\/strong><\/p>\n<p>using chatServer.Properties; using Photon.SocketServer; using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace chatServer {     \/\/\/ &lt;summary&gt;     \/\/\/ \u7ee7\u627fapplicationBase\u7684\u7c7b\u662f\u5165\u53e3\u7a0b\u5e8f\uff0c\u4e5f\u662f\u542f\u52a8\u7a0b\u5e8f     \/\/\/ &lt;\/summary&gt;     public class ChartServer : ApplicationBase     {         \/\/\/ &lt;summary&gt;         \/\/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u8fd9\u4e2aServer\u7aef\u8c03\u7528         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;param name=&quot;initRequest&quot;&gt;&lt;\/param&gt;         \/\/\/ &lt;returns&gt;&lt;\/returns&gt;         protected override PeerBase CreatePeer(InitRequest initRequest)         {             return new ChartPeerBase(initRequest.Protocol, initRequest.PhotonPeer);         }          \/\/\/ &lt;summary&gt;         \/\/\/ Server\u7aef\u542f\u52a8\u65f6\u8c03\u7528         \/\/\/ &lt;\/summary&gt;         protected override void Setup()         {          }          \/\/\/ &lt;summary&gt;         \/\/\/ \u8fd9\u4e2aServer\u7aef\u505c\u6389\u65f6\u8c03\u7528         \/\/\/ &lt;\/summary&gt;         protected override void TearDown()         {          }     } }<\/p>\n<p><strong>PeerBase<\/strong><\/p>\n<p>using Photon.SocketServer; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using PhotonHostRuntimeInterfaces;  namespace chatServer.Properties {     \/\/\/ &lt;summary&gt;     \/\/\/ \u7528\u6765\u548c\u5ba2\u6237\u7aef\u8fdb\u884c\u901a\u4fe1     \/\/\/ &lt;\/summary&gt;     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();         }     } }<\/p>\n<p><strong>\u8bbe\u7f6e\u8f93\u51fa<\/strong> <br \/>\u5728\u4e0b\u8f7d\u597d\u7684sdk\u76ee\u5f55\u4e0b\u65b0\u5efa\u6587\u4ef6MyChartServer\uff0c\u5e76\u5728\u6587\u4ef6\u4e0b\u65b0\u5efabin\u76ee\u5f55<\/p>\n<p>\u9879\u76ee-chatserver\u5c5e\u6027\uff08Alt F7\uff09-\u751f\u6210-\u8f93\u51fa\u8def\u5f84\u4e3aExitGames-Photon-Server-SDK_v3-4-31-10808deployMyChartServerbin<\/p>\n<p><strong>\u914d\u7f6e\u6587\u4ef6<\/strong> <br \/>ExitGames-Photon-Server-SDK_v3-4-31-10808deploybin_Win64\u914d\u7f6ePhotonServer.config\u6587\u4ef6\u4e0b<\/p>\n<p>Application Name=&rdquo;Lite&rdquo;\u548c&rdquo;LiteLobby&rdquo;\u95f4\u52a0\u5165\u65b0\u7684Application\uff0c\u914d\u7f6e\u5982\u4e0b<\/p>\n<p>&lt;Applications Default=&quot;Lite&quot;&gt;               &lt;!&#8211; Lite Application &#8211;&gt;             &lt;Application                 Name=&quot;Lite&quot;                 BaseDirectory=&quot;Lite&quot;                 Assembly=&quot;Lite&quot;                 Type=&quot;Lite.LiteApplication&quot;                 ForceAutoRestart=&quot;true&quot;                 WatchFiles=&quot;dll;config&quot;                 ExcludeFiles=&quot;log4net.config&quot;&gt;             &lt;\/Application&gt;        &lt;!&#8211; ChartServer Application &#8211;&gt;           &lt;Application             Name=&quot;ChartServer&quot;\/\/\u81ea\u5b9a\u4e49Name             BaseDirectory=&quot;MyChartServer&quot;\/\/\u6587\u4ef6\u76ee\u5f55\uff0c\u5982\u6211\u7684\u6587\u4ef6\u5728deploy\u4e0bMyChartServer\u6587\u4ef6\u5939\u4e0b             Assembly=&quot;chatServer&quot;\/\/\u7ee7\u627fApplicationBase\u7684\u7c7b\u6240\u5728\u547d\u540d\u7a7a\u95f4             Type=&quot;chatServer.ChartServer&quot;\/\/\u7ee7\u627fApplicationBase\u7684\u7c7b\u6240\u5728\u547d\u540d\u7a7a\u95f4 \u7c7b\u540d             ForceAutoRestart=&quot;true&quot;             WatchFiles=&quot;dll;config&quot;             ExcludeFiles=&quot;log4net.config&quot;&gt;           &lt;\/Application&gt;              &lt;!&#8211; LiteLobby Application &#8211;&gt;             &lt;Application                 Name=&quot;LiteLobby&quot;                 BaseDirectory=&quot;LiteLobby&quot;                 Assembly=&quot;LiteLobby&quot;                 Type=&quot;LiteLobby.LiteLobbyApplication&quot;                 ForceAutoRestart=&quot;true&quot;                 WatchFiles=&quot;dll;config&quot;                 ExcludeFiles=&quot;log4net.config&quot;&gt;             &lt;\/Application&gt;<\/p>\n<p><strong>\uff08\u4e8c\uff09\u5ba2\u6237\u7aef<\/strong> <br \/><strong>\u65b0\u5efa\u63a7\u5236\u53f0\u7a0b\u5e8f<\/strong> <br \/><strong>\u5f15\u7528<\/strong>    Photon3DotNet<\/p>\n<p>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(&quot;Connect&quot;);                         break;             }         }     }      class Program     {         static void Main(string[] args)         {             ChatServerListener listener=new ChatServerListener();             PhotonPeer peer=new PhotonPeer(listener,ConnectionProtocol.Tcp);\/\/\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u9009\u62e9\u7684\u534f\u8bae             peer.Connect(&quot;127.0.0.1:4530&quot;, &quot;ChartServer&quot;);\/\/\u8fde\u63a5\u670d\u52a1\u5668\uff0c4530\u4e3aconfig\u6587\u4ef6\u6307\u5b9a\u534f\u8bae\u6240\u5bf9\u5e94\u7684\u7aef\u53e3\u53f7\uff0cChartserver\u4e3a\u4e4b\u524d\u7684\u670d\u52a1\u7aef\u5e94\u7528             while (!listener.isconnect)\/\/\u5224\u65ad\u662f\u5426\u5efa\u7acb\u8fde\u63a5             {                 peer.Service();\/\/\u8fd9\u4e2a\u8c03\u7528\u5b8c\u624d\u80fd\u5411\u670d\u52a1\u5668\u53d1\u8d77\u8bf7\u6c42             }          }     } }<\/p>\n<p><strong>\uff08\u4e09\uff09\u8fd0\u884c\u5ba2\u6237\u7aef\u670d\u52a1\u7aef\uff0c\u5b8c\u6210\u8fde\u63a5<\/strong> <br \/><strong>\uff08\u56db\uff09\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42<\/strong> <br \/>\u4fee\u6539\u4e4b\u524d\u7684\u5ba2\u6237\u7aef<\/p>\n<p>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(&quot;Connect&quot;);                         break;             }         }     }      class Program     {         static void Main(string[] args)         {             ChatServerListener listener=new ChatServerListener();             PhotonPeer peer=new PhotonPeer(listener,ConnectionProtocol.Tcp);             peer.Connect(&quot;127.0.0.1:4530&quot;, &quot;ChartServer&quot;);\/\/\u8fde\u63a5\u670d\u52a1\u5668             while (!listener.isconnect)             {                 peer.Service();             }             Dictionary&lt;byte,object&gt; dict=new Dictionary&lt;byte, object&gt;();             dict.Add(1,&quot;username&quot;);             dict.Add(2,&quot;password&quot;);             peer.OpCustom(1, dict, true);             while (true)             {                 \/\/\u6b7b\u5faa\u73af\u9632\u6b62\u7a0b\u5e8f\u7ec8\u6b62                  peer.Service();\/\/\u53d1\u51fa\u53bb             }         }     } }<\/p>\n<p><strong>\uff08\u4e94\uff09\u670d\u52a1\u7aef\u54cd\u5e94<\/strong> <br \/>\u4fee\u6539\u4e4b\u524d\u7684\u670d\u52a1\u7aefPeerBase<\/p>\n<p>\/\/\/ \u5f53\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\u65f6\u8c03\u7528         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;param name=&quot;operationRequest&quot;&gt;&lt;\/param&gt;         \/\/\/ &lt;param name=&quot;sendParameters&quot;&gt;&lt;\/param&gt;         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)         {             Dictionary&lt;byte, object&gt; dict = new Dictionary&lt;byte, object&gt;();             dict.Add(1,&quot;siki&quot;);             OperationResponse response=new OperationResponse(1,dict);             SendOperationResponse(response, sendParameters);         }     }<\/p>\n<p><strong>\uff08\u516d\uff09\u5ba2\u6237\u7aef\u83b7\u5f97\u670d\u52a1\u7aef\u7684\u54cd\u5e94<\/strong><\/p>\n<p>\/\/\/ &lt;summary&gt;         \/\/\/ \u5f97\u5230\u670d\u52a1\u5668\u7aef\u7684\u54cd\u5e94         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;param name=&quot;operationResponse&quot;&gt;&lt;\/param&gt;         public void OnOperationResponse(OperationResponse operationResponse)         {             Dictionary&lt;byte, object&gt; dict = operationResponse.Parameters;             object val = null;             dict.TryGetValue(1,out val);             Console.WriteLine(&quot;getserver&quot; val.ToString());         }<\/p>\n<p><strong>\uff08\u4e03\uff09unity\u5ba2\u6237\u7aef<\/strong> <br \/>\u76ee\u5f55Plugin\u4e0b\u5f15\u5165sdk\/lib\/Photon3Unity3D.dll<\/p>\n<p>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(&quot;127.0.0.1:4530&quot;, &quot;ChartServer&quot;);\/\/\u8fde\u63a5\u670d\u52a1\u5668     }      void Update()     {         peer.Service();       }      void OnGUI()     {         if (isconnect)         {             if (GUILayout.Button(&quot;Send&quot;))             {                 Dictionary&lt;byte, object&gt; dict = new Dictionary&lt;byte, object&gt;();                 dict.Add(1, &quot;username&quot;);                 dict.Add(2, &quot;password&quot;);                 peer.OpCustom(1, dict, true);             }          }     }      public void DebugReturn(DebugLevel level, string message)     {         Debug.Log(level &quot;:&quot; message);     }      public void OnEvent(EventData eventData)     {      }      public void OnOperationResponse(OperationResponse operationResponse)     {         Dictionary&lt;byte, object&gt; dict = operationResponse.Parameters;         object val = null;         dict.TryGetValue(1, out val);         Debug.Log(&quot;getserver&quot;   val.ToString());     }      public void OnStatusChanged(StatusCode statusCode)     {         switch (statusCode)         {             case StatusCode.Connect:                 isconnect = true;                 Debug.Log(&quot;Connect&quot;);                 break;         }      } }<\/p>\n<p>\u66f4\u591a\u5173\u4e8e\u4e91\u670d\u52a1\u5668\uff0c<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/domain.html\" title=\"\u57df\u540d\u6ce8\u518c\" target=\"_blank\">\u57df\u540d<\/a><\/span>\u6ce8\u518c\uff0c\u865a\u62df\u4e3b\u673a\u7684\u95ee\u9898\uff0c\u8bf7\u8bbf\u95ee<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">\u57a6\u6d3e\u79d1\u6280<\/a><\/span>\u5b98\u7f51\uff1a<span class='wp_keywordlink'><a href=\"https:\/\/www.kenpai.com\/\" title=\"\u57a6\u6d3e\u79d1\u6280\u5b98\u7f51\" target=\"_blank\">www.kenpai.com<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff08\u4e00\uff09\u670d\u52a1\u7aef \u65b0\u5efa\u7c7b\u5e93 \u5f15\u7528\uff1aExitGamesLibs,Photon.SocketServer,PhotonHostRuntimeInterfaces<\/p>\n","protected":false},"author":2,"featured_media":4350,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3322],"tags":[3928],"class_list":["post-5060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ktougao","tag-unityphoton-server"],"_links":{"self":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/comments?post=5060"}],"version-history":[{"count":0,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/5060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media\/4350"}],"wp:attachment":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media?parent=5060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/categories?post=5060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/tags?post=5060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}