Photon服务器引擎 入门教程一

Stella981
• 阅读 430

首先去 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. namespace MyServer

  6. {

  7. public class MyPeer : PeerBase

  8. {

  9. public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)

  10. : base(protocol, photonPeer)

  11. {

  12. }

  13. protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)

  14. {

  15. }

  16. protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)

  17. {

  18. }

  19. }

  20. }

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. namespace MyServer

  7. {

  8. public class MyApplication : ApplicationBase

  9. {

  10. protected override PeerBase CreatePeer(InitRequest initRequest)

  11. {

  12. return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);

  13. }

  14. protected override void Setup()

  15. {

  16. }

  17. protected override void TearDown()

  18. {

  19. }

  20. }

  21. }

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位机器,就选择这个),添加以下配置:

[csharp] 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">

这段代码放在放这里节点下面

Name:项目名字

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

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

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

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

WatchFiles和ExcludeFiles

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

(证书放在deploy\bin_Win64\目录下)

下面开始编写客户端代码,首先从官网下载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. using ExitGames.Client.Photon;

  4. public class TestConnection : MonoBehaviour,IPhotonPeerListener {

  5. public PhotonPeer peer;

  6. // Use this for initialization

  7. void Start () {

  8. peer = new PhotonPeer(this,ConnectionProtocol.Udp);

  9. }

  10. // Update is called once per frame

  11. void Update () {

  12. peer.Service();

  13. }

  14. void OnGUI(){

  15. if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){

  16. peer.Connect("localhost:5055","MyServer");

  17. }

  18. }

  19. #region IPhotonPeerListener implementation

  20. public void DebugReturn (DebugLevel level, string message)

  21. {

  22. }

  23. public void OnOperationResponse (OperationResponse operationResponse)

  24. {

  25. }

  26. public void OnStatusChanged (StatusCode statusCode)

  27. {

  28. switch(statusCode){

  29. case StatusCode.Connect:

  30. Debug.Log("Connect Success!");

  31. break;

  32. case StatusCode.Disconnect:

  33. Debug.Log("Disconnect!");

  34. break;

  35. }

  36. }

  37. public void OnEvent (EventData eventData)

  38. {

  39. }

  40. #endregion

  41. }

using UnityEngine;  
using System.Collections;  
  
using ExitGames.Client.Photon;  
  
public class TestConnection : MonoBehaviour,IPhotonPeerListener {  
    public PhotonPeer peer;  
    // Use this for initialization  
    void Start () {  
        peer = new PhotonPeer(this,ConnectionProtocol.Udp);  
    }  
      
    // Update is called once per frame  
    void 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 implementation  
    public 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!".

原文地址:http://blog.csdn.net/dyc333236081818/article/details/8572969?reload

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这