Photon——Hello World Part 1 第一部分

 

Hello World Part 1 第一部分

     The purpose of this tutorial is to give you an introduction to the client API and some of the basic concepts behind Photon. The tutorial is divided in parts. We begin with a very simple demo which will be refactored to more usable code step by step while digging deeper into the different concepts of Photon.
     本教程的目的是给你介绍客户端API和一些Photon的基本概念。本教程是分为几个部分。 我们从一个非常简单的演示开始,然后重构更多可用的代码一步一步的深入。

Contents 内容

Overview 概述

     The application we will create is a simple windows console application that connects to a locally hosted photon server.
     我们将创建的简单的windows控制台程序连接一个本地的Photon服务器
 
     Note: We assume you have followed the steps in   "Photon in 5 Minutes"  and have a locally running Photon Server.
     注意:我们假设您遵循“Photon 5分钟”的步骤和有一个本地运行的Photon服务器

     The basic concepts we will build the application on are:

  • To communicate with Photon Server we need a PhotonPeer where you call Connect to initiate the communication.
  • Outgoing/Incoming messages are queued in the PhotonPeer. In order to transfer the messages over the wire and dispatch (incoming) them to your application Service has to be called.
  • Changes in the status of the connection will be notified to the IPhotonListener.OnStatusChanged.
     我们将构建的应用程序的基本概念是:
  • 我们需要一个 PhotonPeer 与Photon服务器进行调用、连接、发起等通信。
  • 即将传出的/传入的消息是在PhotonPeer排队。 为了将消息传递和调度(输入)他们,你的应用程序服务也会被调用。
  • 改变连接的状态将被通知到 IPhotonListener.OnStatusChanged 。

Project Setup 项目设置

     We will start with a C#/Windows/Console project and name it Helloworld1:
     我们将从一个C#/Windows/Console项目开始,它的名字叫做Helloworld1
 

Photon Server: Hello World New Projekt

     We need to add a reference to PhotonDotNet.dll:
     我们需要去添加引用PhotonDotNet.dll

Photon Server: Hello World Add References

     Select the folder where you extracted the Photon-Dotnet SDK and browse to lib/debug where you will find PhotonDotNet.dll
     选择的文件夹是你提取Photon-Dotnet SDK的地方和浏览到lib /debug,你会发现PhotonDotNet.dll
 

Photon Server: Hello World Browse References

     The next step ist to add “using ExitGames.Client.Photon;”
     下一步是添加 “using ExitGames.Client.Photon;”
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExitGames.Client.Photon;
 
     Note: without the using line you will get compile errors like: "The type or namespace name 'IPhotonPeerListener' could not be found (are you missing a using directive or an assembly reference?)...".And the "Implement interface 'IPhotonPeerListener'" Intellisense help (see below) will be missing. In our   forums  we've got the following hint: "For Mac / MonoDevelop users need to reference System.Core to get .linq". We don't require Linq for this tutorial so you may as well just remove the line.
     注意:没有using你会得到编译错误,如:“类型或名称空间的名字“IPhotonPeerListener”不能被发现(你错失了一个using指令或一个程序集引用)……”。和“实现接口的IPhotonPeerListener’”智能感知帮助(见下文)将会丢失。在我们的论坛我们已经得到了下面的提示:“Mac / MonoDevelop用户需要参考系统。核心得到linq”。 我们不需要Linq本教程所以你不妨就删除行。

OnStatusChanged OnStatusChanged事件

     Now we will implement the listener where we receive the PhotonPeer notifications. So the next step is adding the interface to the   Class Program:
     现在我们将实现侦听器,为了我们能够收到PhotonPeer通知。所以下一步是为类程序添加接口:

Photon Server: Hello World Add Interface

     For now we will only look into   OnStatusChanged  where status changes of the PhotonPeer are notified and ignore the other three callbacks. So next we will replace the NotImplementedException as highlighted below:
     现在我们只会关注 OnStatusChanged 在状态更改时 PhotonPeer的通知,而忽视其他三个回调。 所以接下来我们将取代NotImplementedException,请看下面:
 
#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

Peer Connect Peer连接

     So now we are getting to the main part of our tutorial. We add line 3 to create an instance of our listener. Line 4 will create an instance of the PhotonPeer and line 5 initiates the server connect to local host on port 5055. “Lite” is the default application on the server.
     所以现在我们获取的是教程的主要部分。 我们添加第3行,我们的侦听器创建一个实例。第4行将创建的一个PhotonPeer实例,第5行启动服务器连接到本地主机上的端口5055。 “Lite”是服务器上默认的应用程序。
 
static void Main(string[] args)
{
    var listener = new Program();
    var peer = new PhotonPeer(listener, ConnectionProtocol.Udp);
    peer.Connect("localhost:5055", "Lite");
    ...
 

Calling Service 调用服务器

     When running the code above - nothing would happen. We have to call   peer.Service  to trigger the network transfer and dispatching of notifications:
     当运行上面的代码——什么事都不会发生。 我们必须调用 peer.Service触发网络传输和调度通知:
 
do
{
    Console.WriteLine(".");
    peer.Service();
    System.Threading.Thread.Sleep(500);
}
while (true);
 
     Ok, we are done. Now hit F5. When Photon Server is running you will see the following:
     好了,我们已经完成了任务。现在按F5。当Photon服务器运行,您将看到以下:
 

Photon Server: Hello World Photon Running

     That’s it. You are now connected to the Photon Server.   In part 2  we will have a look on how to start using the connection to the Photon Server.
     就是这样。您现在已连接到Photon服务器。在第2部分中我们将看一看如何开始连接到Photon服务器。
 

Photon Server not found Photon服务未找到

     If Photon Server is not running you will get a disconnect notification:
     如果Photon服务器没有运行,你将会得到一个断开通知:

Photon Server: Hello World Disconnect Notification

     In the screenshot above you see the behavior when trying to connect against a locally hosted Photon Server.OnStatusChanged:InternalReceiveException: The client is trying to connect a not existent service on the same machine, because the windows socket lib raises an exception.A disconnect, triggered after a timeout, is the normal behavior when connecting to a remote server where photon is not running:
     在上面的截图里,你看到的是尝试连接本地服务器托管的Photon。OnStatusChanged:InternalReceiveException:客户端尝试连接一个不存在的服务在同一台机器上,因为windows socket引发一个异常。一个断开,导致了超时,当连接到一个远程服务器但Photon不运行时这是正常的行为:

Photon Server: Hello World Disconnect triggered after timeout

     You can try this out by simply connecting to a site you know it’s not running photon, “google.com” for instance. Connect  returns false if the hostname is unknown. The state of the peer doesn’t change to connecting.
     你可以试试连接到一个网站,它不是Photon,比如“google.com”。如果主机名是未知的连接返回false。 peer的状态不会改变为连接
 
     To see the host unknown behavior, change your code as follows:
     看到主机未知的行为,改变你的代码如下:
 
        //peer.Connect("localhost: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!");
 

Photon Server: Hello World Unknown Hostname

Final Demo Code 最后的演示代码

     Copy the code section below and replace the contents of your Program.cs in your project.
     复制下面的代码段和替换您的程序的内容,cs在您的项目。
 
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
     }
}

你可能感兴趣的:(Hello world)