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

相关文章

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

基于Python打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1