Photon服务器引擎 入门教程一

2024-04-17 14:18

本文主要是介绍Photon服务器引擎 入门教程一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Photon是个好东西,但是网上的入门教程太少了,特别是中文版的。小弟就自己琢磨吧,下面一系列是对Photon的研究过程,如有哪个地方写的有误,望请前辈指教。

首先去PhotonServer SDK下载服务器端SDK,需要登录的,就先注册一个账号吧.

解压出来是四个文件


deploy:主要存放photon的服务器控制程序和服务端Demo

doc:顾名思义,文档

lib:Photon类库,开发服务端需要引用的

src-server:服务端Demo源代码


今天搞一个客户端连接服务器最简单的程序,也算是hello world吧

客户端以Unity3d 为基础,hello world包括配置服务器,客户端,客户端连接服务器,客户端状态改变。

第一步:配置服务器端

打开deploy文件夹,看到包含了不同平台编译出的Photon目录,以“bin_”为前缀命名目录,选择你的电脑对应的文件夹打开,看到PhotonControl.exe,运行后,可以在windows右下角看到一个图标,点击图标可以看到photon服务器控制菜单,这个以后再做详细介绍.


打开visual stadio,新建项目,选择c# 类库,应用程序名字叫做MyServer.

完成后,把我们的Class1.cs,改名为MyApplication.cs,作为服务器端主类.然后在当前项目添加引用,链接到刚才提到的lib文件夹目录下,添加以下引用:

ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll

然后新建一个类:MyPeer.cs,写法如下:

[csharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using Photon.SocketServer;  
  4. using PhotonHostRuntimeInterfaces;  
  5.   
  6. namespace MyServer  
  7. {  
  8.     public class MyPeer : PeerBase  
  9.     {  
  10.   
  11.         public  MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)  
  12.             : base(protocol, photonPeer)  
  13.         {   
  14.               
  15.         }  
  16.   
  17.         protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)  
  18.         {  
  19.               
  20.         }  
  21.   
  22.         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)  
  23.         {  
  24.               
  25.         }  
  26.     }  
  27. }  
using System;
using System.Collections.Generic;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;namespace MyServer
{public class MyPeer : PeerBase{public  MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer): base(protocol, photonPeer){ }protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail){}protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters){}}
}

接上,MyApplication.cs类这样写:

[csharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Photon.SocketServer;  
  6.   
  7. namespace MyServer  
  8. {  
  9.     public class MyApplication : ApplicationBase  
  10.     {  
  11.   
  12.         protected override PeerBase CreatePeer(InitRequest initRequest)  
  13.         {  
  14.             return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);  
  15.         }  
  16.   
  17.         protected override void Setup()  
  18.         {  
  19.               
  20.         }  
  21.   
  22.         protected override void TearDown()  
  23.         {  
  24.               
  25.         }  
  26.     }  
  27. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;namespace MyServer
{public class MyApplication : ApplicationBase{protected override PeerBase CreatePeer(InitRequest initRequest){return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);}protected override void Setup(){}protected override void TearDown(){}}
}


完成后,在解决方案资源管理器中选中当前项目,打开属性,选择生成选项卡,把输出路径改成bin\,然后就生成类库吧

复制当前项目下MyServer文件夹到deploy文件夹下,删除除了bin文件夹以外其他所有文件和文件夹,然后文本编辑器打开deploy\bin_Win64\PhotonServer.config配置文件(我的是win7 64位机器,就选择这个),添加以下配置:


[html] view plain copy print ?
  1. <Application  
  2.                 Name="MyServer"  
  3.                 BaseDirectory="MyServer"  
  4.                 Assembly="MyServer"  
  5.                 Type="MyServer.MyApplication"  
  6.                 EnableAutoRestart="true"  
  7.                 WatchFiles="dll;config"  
  8.                 ExcludeFiles="log4net.config">  
<ApplicationName="MyServer"BaseDirectory="MyServer"Assembly="MyServer"Type="MyServer.MyApplication"EnableAutoRestart="true"WatchFiles="dll;config"ExcludeFiles="log4net.config">
Name:项目名字

BaseDirectory:根目录,deploy文件夹下为基础目录

Assembly :是在生成的类库中的bin目录下与我们项目名称相同的.dll文件的名字

Type:是主类的全称,在这里是:MyServer.MyApplication,一定要包括命名空间

EnableAutoRestart:是否是自动启动,表示当我们替换服务器文件时候,不用停止服务器,替换后photon会自动加载文件

WatchFiles和ExcludeFiles

这段代码放在<Default><Applications>放这里</Applications></Default>节点下面

完成后保存,运行托盘程序deploy\bin_Win64\PhotonControl.exe,

运行它,如果托盘图标没有变灰,说明服务器运行成功。



下面开始编写客户端代码,首先从官网下载Unity SDK

打开Unity3d编辑器,首先把Photon-Unity3D_v3-0-1-14_SDK\libs\Release\Photon3Unity3D.dll导入到Unity中,新建脚本TestConnection.cs,脚本代码如下:

[csharp] view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. using ExitGames.Client.Photon;  
  5.   
  6. public class TestConnection : MonoBehaviour,IPhotonPeerListener {  
  7.     public PhotonPeer peer;  
  8.     // Use this for initialization  
  9.     void Start () {  
  10.         peer = new PhotonPeer(this,ConnectionProtocol.Udp);  
  11.     }  
  12.       
  13.     // Update is called once per frame  
  14.     void Update () {  
  15.         peer.Service();  
  16.     }  
  17.       
  18.     void OnGUI(){  
  19.         if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){  
  20.             peer.Connect("localhost:5055","MyServer");  
  21.         }  
  22.     }  
  23.  
  24.     #region IPhotonPeerListener implementation  
  25.     public void DebugReturn (DebugLevel level, string message)  
  26.     {  
  27.           
  28.     }  
  29.   
  30.     public void OnOperationResponse (OperationResponse operationResponse)  
  31.     {  
  32.           
  33.     }  
  34.   
  35.     public void OnStatusChanged (StatusCode statusCode)  
  36.     {  
  37.         switch(statusCode){  
  38.         case StatusCode.Connect:  
  39.             Debug.Log("Connect Success!");  
  40.             break;  
  41.         case StatusCode.Disconnect:  
  42.             Debug.Log("Disconnect!");  
  43.             break;  
  44.         }  
  45.     }  
  46.   
  47.     public void OnEvent (EventData eventData)  
  48.     {  
  49.           
  50.     }  
  51.     #endregion  
  52. }  
using UnityEngine;
using System.Collections;using ExitGames.Client.Photon;public class TestConnection : MonoBehaviour,IPhotonPeerListener {public PhotonPeer peer;// Use this for initializationvoid Start () {peer = new PhotonPeer(this,ConnectionProtocol.Udp);}// Update is called once per framevoid Update () {peer.Service();}void OnGUI(){if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){peer.Connect("localhost:5055","MyServer");}}#region IPhotonPeerListener implementationpublic void DebugReturn (DebugLevel level, string message){}public void OnOperationResponse (OperationResponse operationResponse){}public void OnStatusChanged (StatusCode statusCode){switch(statusCode){case StatusCode.Connect:Debug.Log("Connect Success!");break;case StatusCode.Disconnect:Debug.Log("Disconnect!");break;}}public void OnEvent (EventData eventData){}#endregion
}

把脚本绑定到场景中物体上,运行后可以看到一个按钮,点击连接,如果连接成功会打印"Connect Success!".

Unity客户端例子到这里下载

这篇关于Photon服务器引擎 入门教程一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/912026

相关文章

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

速了解MySQL 数据库不同存储引擎

快速了解MySQL 数据库不同存储引擎 MySQL 提供了多种存储引擎,每种存储引擎都有其特定的特性和适用场景。了解这些存储引擎的特性,有助于在设计数据库时做出合理的选择。以下是 MySQL 中几种常用存储引擎的详细介绍。 1. InnoDB 特点: 事务支持:InnoDB 是一个支持 ACID(原子性、一致性、隔离性、持久性)事务的存储引擎。行级锁:使用行级锁来提高并发性,减少锁竞争

速盾:直播 cdn 服务器带宽?

在当今数字化时代,直播已经成为了一种非常流行的娱乐和商业活动形式。为了确保直播的流畅性和高质量,直播平台通常会使用 CDN(Content Delivery Network,内容分发网络)服务器来分发直播流。而 CDN 服务器的带宽则是影响直播质量的一个重要因素。下面我们就来探讨一下速盾视角下的直播 CDN 服务器带宽问题。 一、直播对带宽的需求 高清视频流 直播通常需要传输高清视频

一种改进的red5集群方案的应用、基于Red5服务器集群负载均衡调度算法研究

转自: 一种改进的red5集群方案的应用: http://wenku.baidu.com/link?url=jYQ1wNwHVBqJ-5XCYq0PRligp6Y5q6BYXyISUsF56My8DP8dc9CZ4pZvpPz1abxJn8fojMrL0IyfmMHStpvkotqC1RWlRMGnzVL1X4IPOa_  基于Red5服务器集群负载均衡调度算法研究 http://ww

RTMP流媒体服务器 crtmpserver

http://www.oschina.net/p/crtmpserver crtmpserver又称rtmpd是Evostream Media Server(www.evostream.com)的社区版本采用GPLV3授权 其主要作用为一个高性能的RTMP流媒体服务器,可以实现直播与点播功能多终端支持功能,在特定情况下是FMS的良好替代品。 支持RTMP的一堆协议(RT

Smarty模板引擎工作机制(一)

深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解。 在没有使用Smarty模板引擎的情况下,我们都是将PHP程序和网页模板合在一起编辑的,好比下面的源代码: <?php$title="深处浅出之Smarty模板引擎工作机制";$content=

云原生之高性能web服务器学习(持续更新中)

高性能web服务器 1 Web服务器的基础介绍1.1 Web服务介绍1.1.1 Apache介绍1.1.2 Nginx-高性能的 Web 服务端 2 Nginx架构与安装2.1 Nginx概述2.1.1 Nginx 功能介绍2.1.2 基础特性2.1.3 Web 服务相关的功能 2.2 Nginx 架构和进程2.2.1 架构2.2.2 Ngnix进程结构 2.3 Nginx 模块介绍2.4

Weex入门教程之4,获取当前全局环境变量和配置信息(屏幕高度、宽度等)

$getConfig() 获取当前全局环境变量和配置信息。 Returns: config (object): 配置对象;bundleUrl (string): bundle 的 url;debug (boolean): 是否是调试模式;env (object): 环境对象; weexVersion (string): Weex sdk 版本;appName (string): 应用名字;

Weex入门教程之3,使用 Vue 开发 Weex 页面

环境安装 在这里简略地介绍下,详细看官方教程 Node.js 环境 Node.js官网 通常,安装了 Node.js 环境,npm 包管理工具也随之安装了。因此,直接使用 npm 来安装 weex-toolkit。 npm 是一个 JavaScript 包管理工具,它可以让开发者轻松共享和重用代码。Weex 很多依赖来自社区,同样,Weex 也将很多工具发布到社区方便开发者使用。