[小明带你玩儿Photon]3.Hello World i

一.写在前面

 本篇将对Photon的客户端API和一些基础的概念进行介绍.本篇分为上下两部分,我们先从一个简单的demo开始,通过一步步的重构它使其成更有用的代码,与此同时深入了解Photon中不同的概念.

二.简介

 我们将创建一个链接到本地主机photon服务的简单的windows控制台应用.构建应用所需的一些基础概念如下:

    • 和服务端通信需要一个PhotonPeer,通过调用其中Connect方法来开始通信.
    • 发出/接收的消息都被存放在PhotonPeer的一个队列中,为了通过网络传递的消息和分派(输入)他们到应用程序,Service()方法需要被调用.
    • 连接状态的改变将会告之IPhotonListener.OnStatusChanged方法.

三.程序创建

通过Visual  Studio创建一个控制台应用程序,命名为HelloWorld1.然后在引用中添加Photon的libs目录下的Photon3DotNet.dll.

在自动打开的脚本中,我们先增加一个命名空间ExitGames.Client.Photon.然后为了我们实现对接收到的PhotonPeer通知的监听,为Program添加借口IPotonPeerListener.Photon Server: Hello World Add Interface首先我们只关注会指明PhotonPeer状态改变的OnStatusChanged方法,不去管其他三个回调.所以我们把NotImpletmentException替换为控制台输出,如下:

#region IPhotonPeerListener 成员 public void DebugReturn(DebugLevel level, string message) { //throw new NotImplementedException(); } public void OnEvent(EventData eventData) { //throw new NotImplementedException(); } public void OnOperationResponse(OperationResponse operationResponse) { //throw new NotImplementedException(); } public void OnStatusChanged(StatusCode statusCode) { Console.WriteLine("OnStatusChanged:" + statusCode); } #endregion

接着我们建立一个peer连接,第三行表示创建一个listener的实例,第四行创建了一个PhotonPeer实例,第五行开始了一个到本机端口4530的服务器连接."Lite"是服务器上默认的程序.

1 static void Main(string[] args)
2 {
3     var listener = new Program();
4     var peer = new PhotonPeer(listener, ConnectionProtocol.Udp);
5     peer.Connect("localhost:4530", "Lite");
6     ...

当运行以上代码时,我们发现什么都没有发生.是因为还没有调用peer.Service()来触发网络传播消息和分派通知.

do
{
    Console.WriteLine(".");
    peer.Service();
    System.Threading.Thread.Sleep(500);
}
while (true);

现在我们完成了,当再次运行代码时,若Photon服务器正在运行,你会看到如下结果:

Photon Server: Hello World Photon Running

这表明我们已经链接到服务器了.

如果没有能连接到服务器,会得到相应的异常提示.

程序的最后代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using ExitGames.Client.Photon;
 
namespace HelloWorld1
{
    class Program : IPhotonPeerListener
    {
        static void Main(string[] args)
        {
            var listener = new Program();
            var peer = new PhotonPeer(listener, ConnectionProtocol.Udp);
 
            if (peer.Connect("localhost:5055", "Lite"))
            //// if (peer.Connect("google.com:5055", "Lite"))
            //// if (peer.Connect("xxx:5055", "Lite"))
            {
                do
                {
                    Console.WriteLine(".");
                    peer.Service();
                    System.Threading.Thread.Sleep(500);
                }
                while (!Console.KeyAvailable);
            }
            else
                Console.WriteLine("Unknown hostname!");
            Console.ReadKey();
        }
 
        #region IPhotonPeerListener Members
        public void DebugReturn(DebugLevel level, string message)
        {
            //throw new NotImplementedException();
        }
 
        public void OnEvent(EventData eventData)
        {
            //throw new NotImplementedException();
        }
 
        public void OnOperationResponse(OperationResponse operationResponse)
        {
            //throw new NotImplementedException();
        }
 
        public void OnStatusChanged(StatusCode statusCode)
        {
            Console.WriteLine("OnStatusChanged:" + statusCode);
        }
        #endregion
    }
}

原文地址:https://www.cnblogs.com/WongSiuming/p/4934303.html