PhotonServer 学习

版本:Photon-OnPremise-Server-SDK_v3-4-31-10808

输出文件夹:deploy/名称/bin

PhotonServer.config 配置

 1 <Applications Default="ChatServer">
 2 <Application
 3     Name="ChatServer"
 4     BaseDirectory="ChatServer"
 5     Assembly="ChatServer"
 6     Type="ChatServer.ChatServer"
 7     ForceAutoRestart="true"
 8     WatchFiles="dll;config"
 9     ExcludeFiles="log4net.config">
10 </Application>
11 </Applications>
View Code

Server端:

引用 ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll

 1 using System;
 2 using Photon.SocketServer;
 3 
 4 namespace ChatServer {
 5     class ChatServer:ApplicationBase {
 6 
 7         //收到客户端请求创建一个新的PeerBase
 8         protected override PeerBase CreatePeer(InitRequest initRequest) {
 9             return new ChatPeer(initRequest.Protocol,initRequest.PhotonPeer);
10         }
11 
12         protected override void Setup() {
13 
14         }
15 
16         protected override void TearDown() {
17 
18         }
19     }
20 }
ChatServer
 1 using System;
 2 using System.Collections.Generic;
 3 using Photon.SocketServer;
 4 using PhotonHostRuntimeInterfaces;
 5 
 6 namespace ChatServer {
 7 
 8     //继承PeerBase
 9     class ChatPeer:PeerBase {
10 
11         //构造函数1
12         public ChatPeer(InitRequest initRequest) : base(initRequest) {
13 
14         }
15 
16         //构造函数2
17         public ChatPeer(IRpcProtocol iRpcProtocol,IPhotonPeer iPhotonPeer) : base(iRpcProtocol,iPhotonPeer) {
18 
19         }
20 
21 
22         protected override void OnDisconnect(DisconnectReason reasonCode,string reasonDetail) {
23 
24         }
25 
26         //收到客户端请求时执行
27         protected override void OnOperationRequest(OperationRequest operationRequest,SendParameters sendParameters) {
28 
29             Dictionary<byte,object> dict = new Dictionary<byte,object>();
30             dict.Add(1,"void");
31 
32             //创建一个新的响应操作,发送给客户端
33             OperationResponse response = new OperationResponse(1,dict);
34             SendOperationResponse(response,sendParameters);
35         }
36     }
37 }
ChatPeer

服务端:

将Photon3Unity3D.dll放到unity Plugin文件夹中

  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using ExitGames.Client.Photon;
  5 
  6 namespace VoidGame {
  7 
  8     public class ChatClient : MonoBehaviour,IPhotonPeerListener {
  9 
 10         private PhotonPeer peer;
 11         private bool m_isConnected;
 12 
 13         void Start() {
 14             peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
 15             peer.Connect("127.0.0.1:4530","ChatServer");
 16 
 17         }
 18 
 19         void Update() {
 20             peer.Service();
 21 
 22         }
 23 
 24         void OnGUI() {
 25             if(m_isConnected) {
 26                 if(GUILayout.Button("Send a operation")) {
 27                     Dictionary<byte,object> dict = new Dictionary<byte,object>();
 28                     dict.Add(1,"username");
 29                     dict.Add(2,"password");
 30                     peer.OpCustom(1,dict,true); //发送数据到服务端
 31                 }
 32             }
 33         }
 34 
 35         public void DebugReturn(DebugLevel level,string message) {
 36             Debug.Log(level + ":" + message);
 37         }
 38 
 39         public void OnEvent(EventData eventData) {
 40         }
 41 
 42         //收到服务端响应,获取数据
 43         public void OnOperationResponse(OperationResponse operationResponse) {
 44             Dictionary<byte,object> dict = operationResponse.Parameters;
 45             object value;
 46             dict.TryGetValue(1,out value);
 47             Debug.Log("Get value from server:" + value);
 48         }
 49 
 50         public void OnStatusChanged(StatusCode statusCode) {
 51             switch(statusCode) {
 52             case StatusCode.Connect:
 53                 m_isConnected = true;
 54                 Debug.Log("Connected Success");
 55                 break;
 56             case StatusCode.Disconnect:
 57                 break;
 58             case StatusCode.Exception:
 59                 break;
 60             case StatusCode.ExceptionOnConnect:
 61                 break;
 62             case StatusCode.SecurityExceptionOnConnect:
 63                 break;
 64             case StatusCode.QueueOutgoingReliableWarning:
 65                 break;
 66             case StatusCode.QueueOutgoingUnreliableWarning:
 67                 break;
 68             case StatusCode.SendError:
 69                 break;
 70             case StatusCode.QueueOutgoingAcksWarning:
 71                 break;
 72             case StatusCode.QueueIncomingReliableWarning:
 73                 break;
 74             case StatusCode.QueueIncomingUnreliableWarning:
 75                 break;
 76             case StatusCode.QueueSentWarning:
 77                 break;
 78             case StatusCode.InternalReceiveException:
 79                 break;
 80             case StatusCode.TimeoutDisconnect:
 81                 break;
 82             case StatusCode.DisconnectByServer:
 83                 break;
 84             case StatusCode.DisconnectByServerUserLimit:
 85                 break;
 86             case StatusCode.DisconnectByServerLogic:
 87                 break;
 88             case StatusCode.TcpRouterResponseOk:
 89                 break;
 90             case StatusCode.TcpRouterResponseNodeIdUnknown:
 91                 break;
 92             case StatusCode.TcpRouterResponseEndpointUnknown:
 93                 break;
 94             case StatusCode.TcpRouterResponseNodeNotReady:
 95                 break;
 96             case StatusCode.EncryptionEstablished:
 97                 break;
 98             case StatusCode.EncryptionFailedToEstablish:
 99                 break;
100             default:
101                 break;
102             }
103         }
104     }
105 }
ChatClient

 

转载于:https://www.cnblogs.com/revoid/p/6246107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值