photon入门
来源:互联网 发布:notes是什么软件 编辑:程序博客网 时间:2024/05/27 01:32
server:
MyserverApplication 类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Photon.SocketServer;namespace MyServer{ public class MyServerApplication : ApplicationBase { protected override PeerBase CreatePeer(InitRequest initRequest) { // 建立連線並回傳給Photon Server return new MyServerPeer(initRequest.Protocol, initRequest.PhotonPeer); } protected override void Setup() { // 初始化GameServer } protected override void TearDown() { // 關閉GameServer並釋放資源 } }}
MyServerPeer:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Photon.SocketServer;using PhotonHostRuntimeInterfaces;namespace MyServer{ public class MyServerPeer : PeerBase { #region 建構與解構式 public MyServerPeer(IRpcProtocol rpcProtocol, IPhotonPeer nativePeer) : base(rpcProtocol, nativePeer) { } #endregion protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail) { // 失去連線時要處理的事項,例如釋放資源 } protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { // 取得Client端傳過來的要求並加以處理 switch (operationRequest.OperationCode) { case 5: { if (operationRequest.Parameters.Count < 2) // 若參數小於2則返回錯誤 { // 返回登入錯誤 OperationResponse response = new OperationResponse(operationRequest.OperationCode) { ReturnCode = (short)2, DebugMessage = "Login Fail" }; SendOperationResponse(response, new SendParameters()); } else { var memberID = (string)operationRequest.Parameters[1]; var memberPW = (string)operationRequest.Parameters[2]; if (memberID == "test" && memberPW == "1234") { int Ret = 1; var parameter = new Dictionary<byte, object> { { 80, Ret }, {1, memberID}, {2, memberPW}, {3, "Kyoku"} // 80代表回傳值, 3代表暱稱 }; OperationResponse response = new OperationResponse(operationRequest.OperationCode, parameter) { ReturnCode = (short)0, DebugMessage = "" }; SendOperationResponse(response, new SendParameters()); } else { OperationResponse response = new OperationResponse(operationRequest.OperationCode) { ReturnCode = (short)1, DebugMessage = "Wrong id or password" }; SendOperationResponse(response, new SendParameters()); } } break; } } } }}
client:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using ExitGames.Client.Photon;namespace EZClient{ class Program : IPhotonPeerListener { public bool ServerConnected; public bool OnLogin; PhotonPeer peer; public Program() { ServerConnected = false; OnLogin = false; this.peer = new PhotonPeer(this, ConnectionProtocol.Udp); } static void Main(string[] args) { new Program().Run(); } public void Run() { if (peer.Connect("localhost:5055", "MyServer")) // 若成功連到Server { do { peer.Service(); // 在主迴圈取得Server傳過來的命令,當有傳命令過來時依命令的類型,EventAction或OperationResult會被觸發 if (ServerConnected && !OnLogin) { Console.WriteLine("\n請輸入會員帳號:"); string memberID = Console.ReadLine(); Console.WriteLine("\n請輸入會員密碼:"); string memberPW = Console.ReadLine(); var parameter = new Dictionary<byte, object> { { (byte)1, memberID.Trim() }, { (byte)2, memberPW.Trim() } // parameter key memberID=1, memberPW=2 }; this.peer.OpCustom(5, parameter, true); // 命令代碼為5 OnLogin = true; } System.Threading.Thread.Sleep(500); // 休息.5秒以免鎖死電腦 } while (true); } else { Console.WriteLine("Unknown hostname!"); } } public void DebugReturn(DebugLevel level, string message) { // 印出Debug的回傳字串 Console.WriteLine("Debug messsage : " + message); } public void OnEvent(EventData eventData) { // 取得Server傳過來的事件 } public void OnOperationResponse(OperationResponse operationResponse) { // 取得Server傳過來的命令回傳 switch (operationResponse.OperationCode) { case (byte)5: // 在此範例是訂5為Login要求 { switch (operationResponse.ReturnCode) { case (short)0: // ReturnCode 0 代表成功 { int Ret = Convert.ToInt16(operationResponse.Parameters[(byte)80]); string memberID = Convert.ToString(operationResponse.Parameters[(byte)1]); string memberPW = Convert.ToString(operationResponse.Parameters[(byte)2]); string Nickname = Convert.ToString(operationResponse.Parameters[(byte)3]); Console.WriteLine("Login Success \nRet={0} \nMemberID={1} \nMemberPW={2} \nNickname={3} \n", Ret, memberID, memberPW, Nickname); break; } case (short)1: // 帳號或密碼錯誤 { Console.WriteLine(operationResponse.DebugMessage); break; } case (short)2: // 傳入的參數錯誤 { Console.WriteLine(operationResponse.DebugMessage); break; } default: { Console.WriteLine(String.Format("不明的ReturnCode : {0}", operationResponse.ReturnCode)); break; } } break; } default: { Console.WriteLine(String.Format("不明的OperationCode : {0}", operationResponse.OperationCode)); break; } } } public void OnStatusChanged(StatusCode statusCode) { // 連線狀態變更的通知 Console.WriteLine("PeerStatusCallback:" + statusCode.ToString()); switch (statusCode) { case StatusCode.Connect: // 若己連線 ServerConnected = true; break; case StatusCode.Disconnect: // 若己離線 ServerConnected = false; break; } } }}
unity3d_client:
using UnityEngine;using System.Collections;using System.Security;using System;using System.Collections.Generic;using ExitGames.Client.Photon;public class PhotonClient : MonoBehaviour, IPhotonPeerListener {public string ServerAddress = "localhost:5055";protected string ServerApplication = "MyServer";protected PhotonPeer peer;public bool ServerConnected;public string memberID = "";public string memberPW = "";public bool LoginStatus;public string getMemberID = "";public string getMemberPW = "";public string getNickname = "";public int getRet = 0;public string LoginResult = "";// Use this for initializationvoid Start () {this.ServerConnected = false;this.LoginStatus = false;this.peer = new PhotonPeer(this, ConnectionProtocol.Udp); this.Connect();}internal virtual void Connect() { try { this.peer.Connect(this.ServerAddress, this.ServerApplication); } catch (SecurityException se) { this.DebugReturn(0, "Connection Failed. " + se.ToString()); } }// Update is called once per framevoid Update () {this.peer.Service();}public void DebugReturn (DebugLevel level, string message){Debug.Log(message);}public void OnOperationResponse (OperationResponse operationResponse){// display operationCodethis.DebugReturn(0, string.Format("OperationResult:" + operationResponse.OperationCode.ToString()));switch (operationResponse.OperationCode){case 5:{if( operationResponse.ReturnCode == 0) // if success{getRet = Convert.ToInt32(operationResponse.Parameters[80]);getMemberID = Convert.ToString(operationResponse.Parameters[1]);getMemberPW= Convert.ToString(operationResponse.Parameters[2]);getNickname= Convert.ToString(operationResponse.Parameters[3]);LoginStatus = true;}else{LoginResult = operationResponse.DebugMessage;LoginStatus = false;}break;}}}public void OnStatusChanged (StatusCode statusCode){this.DebugReturn(0, string.Format("PeerStatusCallback: {0}", statusCode));switch (statusCode) { case StatusCode.Connect: this.ServerConnected = true; break; case StatusCode.Disconnect: this.ServerConnected = false; break;}}public void OnEvent (EventData eventData){throw new System.NotImplementedException ();}void OnGUI() {GUI.Label(new Rect(30,10,400, 20), "EZServer Unity3D Test");if( this.ServerConnected ){GUI.Label(new Rect(30,30,400, 20), "Connected");GUI.Label(new Rect(30,60,80, 20), "MemberID:");memberID = GUI.TextField(new Rect(110, 60, 100, 20), memberID, 10);GUI.Label(new Rect(30,90,80, 20), "MemberPW:");memberPW = GUI.TextField(new Rect(110, 90, 100, 20), memberPW, 10);if( GUI.Button( new Rect(30,120,100,24 ), "Login") ){ var parameter = new Dictionary<byte, object> { { (byte)1, memberID }, { (byte)2, memberPW } // parameter key memberID=1, memberPW=2 }; this.peer.OpCustom(5, parameter, true); // operationCode is 5}if( LoginStatus ){GUI.Label(new Rect(30,150,400, 20), "Your MemberID : " + getMemberID);GUI.Label(new Rect(30,170,400, 20), "Your Password : " + getMemberPW);GUI.Label(new Rect(30,190,400, 20), "Your Nickname : " + getNickname);GUI.Label(new Rect(30,210,400, 20), "Ret : " + getRet.ToString());}else{GUI.Label(new Rect(30,150,400, 20), "Please Login");GUI.Label(new Rect(30,170,400, 20), LoginResult);}}else{GUI.Label(new Rect(30,30,400, 20), "Disconnect");}}}
0 0
- photon入门
- [Unity&photon]photon chat入门
- Photon服务器引擎入门
- Photon服务器入门二
- PHOTON基础入门
- [Unity&photon&多人游戏插件]photon入门
- Unity连Photon服务器入门详解
- Photon Server4.0版本入门(一)初识Photon Server 4.0
- Photon Server4.0版本入门(二)创建一个Photon的ServerDemo
- photon引擎
- Photon术语
- Photon介绍
- Photon cloud
- Photon Server
- photon引擎
- Photon Server
- Photon 简介
- Photon服务器引擎(一)Photon简介
- Java设计模式——Observer(观察者)模式
- POJ 1753 Flip Game(暴力+搜索递归)
- HOOK 文件保护,隐藏 禁止访问
- ios 安卓锁屏
- Android 获取手机联系人
- photon入门
- 最受欢迎的五大bug管理平台
- 平安科技移动开发二队技术周报(第一期)
- android: baselineAligned属性认识及用途
- ios pop 折叠动画
- 一次Wi-Fi入侵实录
- 编程迷茫时随手而写
- 如何在eclipse上添加c语言工程头文件
- java圣经《java 编程思想》