Photon开发实战(2)——开发框架、第一个Photon程序

Photon基础开发框架

Photon (v4)的基本框架。开发框架主要Photon和游戏逻辑(C#)两个部分,如下图最新的Photon v4支持的4种底层协议,游戏开发逻辑Photon目前主要划分为Load Balancing 和MMO(大型多人同时在线游戏)。

一、Photon服务端示例

1、服务端开发

新建解决方案TestPhotonServer并新建类库项目MyPhotonServer,类库添加Photon引用(可在photon安装目录的lib里找到)

Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll

为什么新建类库项目呢?所有的Photon的服务端程序都是先编译成dll,再由PhotonControl.exe通过配置文件调用运行的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;

namespace MyPhotonServer
{
    public class MyServerApplication:ApplicationBase
    {
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new MyServerPeer(initRequest);
        }

        protected override void Setup()
        {
            //初始化
            
        }

        protected override void TearDown()
        {
            //关闭
        }
    }
}
MyServerApplication
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace MyPhotonServer
{
    public class MyServerPeer:ClientPeer
    {
        public MyServerPeer(InitRequest initRequest)
            : base(initRequest)
        {
            
        }
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            //响应客户端的断开连接
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            //响应客户端的操作请求

        }
    }
}
MyServerPeer

看代码,这里是一个最简单的Photon服务端:

1、Application为服务端程序入口,所有开发者自己的程序都要有一个继承ApplicationBase的类的程序入口

2、Peer为服务端与客户端的对等通信点,服务端和客户端都通过各自的Peer进行通信.

3、V4版本的Photon对.Net的开发API做了调整,在原来的PeerBase基础上又更加细化出不同分工的Peer,这里调用ClientPeer,可以看到官方源码里并ClientPeer并没有什么东西,细心的朋友可以思考为什么这么做

public abstract class ClientPeer : PeerBase
{
    // Methods
    protected ClientPeer(InitRequest initRequest) : base(initRequest)
    {
    }
}

2、服务端部署

1、PhotonControl.exe:首先所有的Photon的服务端程序都是先编译成dll,再通过配置由PhotonControl.exe调用运行的。

2、PhotonServer.config文件:PhotonControl的运行目录中会找到这个文件,主要进行配置开发者程序来给PhotonControl调用。

3、log:PhotonControl.exe会在运行根目录生成日志,另外会在deploy下生成所有服务端的一个日志文件log。

部署:

deploy目录下建一个文件夹TestPhotonServer, 右键我们的服务端类库项目属性将Release的dll重定向下。注意要把dll设置在TestPhotonServer里新建的bin目录里

(由于示例很简单Release时候Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll、ExitGamesLibs.dll这几个dll没有发布到bin,这里手动复制到deploy下的TestPhotonServer/bin里面)

配置PhotonServer.config

 添加Application节点到PhotonServer.config的Applications下,这里我放到loadBlancing下的Applications

      <Application
                Name="TestPhotonServer"
                BaseDirectory="TestPhotonServer"
                Assembly="MyPhotonServer"
                Type="MyPhotonServer.MyServerApplication"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config">
      </Application>        

Name:服务器程序名称

BaseDirectory:设置的是deploy目录为基础设置,这里服务端程序文件夹在deploy里的TestPhotonServer

Assembly:Application入口程序所在的namespace

Type:入口类的完整限定性名称

ForceAutoRestart:顾名思义强制重启

WatchFiles:调用的文件后缀,dll和config

ExcludeFiles:一般是日志配置文件名称

 运行PhotonControl.exe的loadBalancing就可以看到自定义的服务端已经运行

二、客户端

客户端暂时用简单的控制台,解决方案下添加一个控制台项目, 添加引用Photon3DotNet.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Client.Photon;

namespace MyPhotonClient
{
    class MyPhotonClientPeerListener : IPhotonPeerListener
    {
        public bool IsConnect = false;

        public void DebugReturn(DebugLevel level, string message)
        {
           
        }

        public void OnEvent(EventData eventData)
        {
            
        }

        public void OnMessage(object messages)
        {
            
        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            //与服务器连接状态发生改变

            Console.WriteLine("当前与服务端连接状态:"+statusCode);

            switch (statusCode)
            {
                case StatusCode.Connect:
                    IsConnect = true;
                    break;

            }
            
        }
    }
}
MyPhotonClientPeerListener
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Client.Photon;

namespace MyPhotonClient
{
    class Program
    {
        static void Main(string[] args)
        {
            MyPhotonClientPeerListener listener = new MyPhotonClientPeerListener();

            PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Udp);

            if (peer.Connect("localhost:5055","MyServer"))
            {
                Console.WriteLine("客户端准备连接请求……");

                while (!listener.IsConnect)
                {
                    Console.WriteLine("连接中……");
                    peer.Service();
                    System.Threading.Thread.Sleep(500);
                    
                }
                Console.WriteLine("已连接……");


                //peer.Disconnect();



                Console.ReadKey();

            }
            else
            {
                Console.Write("未找到服务器");
            }

        }
        
    }
}
Program

1、IPhotonPeerListener接口主要有5个方法

DebugReturn方法:主要提供各类错误与警告【供开发者】查看,在开发状态下协助开发者纠错。比如:讲上面客户端Program.cs里的地址localhost:5055,改成localhost:5050运行的时候还是会不停的请求,但是无法成功连接,程序是不会报错的。这个时候我们在DebugReturn方法里打印一下message帮助查找问题源

OnEvent(EventData eventData):处理Photon Server发送过来给客户端处理的事件。Event用于客户端和服务端沟通,操作(Operation)通常会触发Event,可以通过Event Code直到事件类型。时间的消息内容通常包含着它的Parameters里。这里暂作简单介绍

OnMessage(object messages):消息回调函数

OnOperationResponse(OperationResponse operationResponse):响应Operation的回调函数,比如加入游戏房间操作,服务器会分配给每个客户端一个编号。这个Client的编号就可以通过响应回调函数获取

OnStatusChanged(StatusCode statusCode):连接状态函数,当游戏的异步操作完成活发生错误时候,状态发生改变回调这个函数

2、PhotonPeer类

PhotonPeer主要功能是客户端和Photon Server 通信。可以理解为对等通信点或者勉强理解为信使。PhotonPeer通过listener和通信协议和服务端通信。。每个Application都可以有多个PhotonPeer,但是每一个不同的PhotonPeer都应该有自己listener用来监听事件、操作、回调函数。这里的listener就是继承IPhotonPeerListener接口的类的实例。

peer.Connect调用的时候并不会直接去连接服务器,只有当peer.service()调用的时候才会向服务器发送请求。 

后文再详解

原文地址:https://www.cnblogs.com/xmai/p/5311765.html