I'm going to assume you have created a project folder under Photon/deploy and created your project inside that folder along with adding the references and renaming the class. For ease of use, I'm still calling my application AegisBorn so update any namespaces or class names to be your Game's class names. I will keep as much of it as generic as possible.
So we have an empty class. Lets start by inheriting from Application. Don't forget that we need to add the using statement for the Application class. After that you can click on Application, go to the little box that appears under the A and hit "implement abstract class Application" and you will see three functions appear, CreatePeer, Setup, and TearDown. Delete the 3 NotImplementedException calls. In Create peer we want to create our Peer object. The following code should be your entire class.
using System;
using Photon.SocketServer;
namespace AegisBorn
{
public class AegisBornApplication : Application
{
protected override IPeer CreatePeer(PhotonPeer photonPeer, InitRequest initRequest)
{
return new AegisBornPeer(photonPeer);
}
protected override void Setup()
{
}
protected override void TearDown()
{
}
}
}
And with that our Application is complete. Next we need to implement our Peer so go ahead and create a new class file in your project and call it GameNamePeer.
This class is our connection to our client, much like our User reference in SmartFoxServer. It contains the functions necessary to pass events back to the client and get operations from the client to have the server process.
We need to inherit from Peer and IOperationHandler. Then you can do like we did before and implement interface IOperationHandler. Again remove the NotImplementedExceptions. From here we will borrow the code that the MMO Demo uses for these functions:
using Photon.SocketServer;
using Photon.SocketServer.Rpc;
namespace AegisBorn
{
class AegisBornPeer : Peer, IOperationHandler
{
public AegisBornPeer(PhotonPeer photonPeer)
: base(photonPeer)
{
this.SetCurrentOperationHandler(this);
}
public void OnDisconnect(Peer peer)
{
this.SetCurrentOperationHandler(OperationHandlerDisconnected.Instance);
this.Dispose();
}
public void OnDisconnectByOtherPeer(Peer peer)
{
peer.ResponseQueue.EnqueueAction(() => peer.RequestQueue.EnqueueAction(() => peer.PhotonPeer.Disconnect()));
}
public OperationResponse OnOperationRequest(Peer peer, OperationRequest operationRequest)
{
return new OperationResponse(operationRequest, 0, "Ok");
}
}
}
Now I'll explain each function. The constructor tells the application that this class will handle all operations.
OnDisconnect sets our handler to be an instance of the disconnected handler. This is done so that if we still receive operations from the client, they will be logged and errors reported back to the client saying they are disconnected. You might be wondering why the server would need to tell us that and it is because we are using UDP which doesn't have a live connection like TCP so we could still send messages even if we are supposed to be disconnected among other things that UDP gives us.
OnDisconnectByOtherPeer is called when another client has said this client needs to be disconnected. this could be done by an admin running on the same server. It queues up an action that causes the peer to disconnect itself.
OnOperationRequest is the function that does everything. Any time the client sends an operation, this function gets called and decides what should be done with that operation. For now we are doing nothing more than sending a message back stating that we received the operation and moves along.
From here you can build the project and then watch Starting Photon to see how you start Photon. The last thing you will need to do is modify the PhotonServer.config and add the application as a server application.
You can follow along with Server Setup Part 1 from the 12:40 mark and they will show you what to modify. For reference, my setup is as follows:
<Applications Default="AegisBorn">
<!-- Lite Application -->
<Application
Name="AegisBorn"
BaseDirectory="AegisBorn"
Assembly="AegisBorn"
Type="AegisBorn.AegisBornApplication"
EnableAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
With all this in place, you can build your project and go to the Photon Control and start the server. Thats all we need to do server side.
From here you can build the project and then watch Starting Photon to see how you start Photon. The last thing you will need to do is modify the PhotonServer.config and add the application as a server application.
You can follow along with Server Setup Part 1 from the 12:40 mark and they will show you what to modify. For reference, my setup is as follows:
<Applications Default="AegisBorn">
<!-- Lite Application -->
<Application
Name="AegisBorn"
BaseDirectory="AegisBorn"
Assembly="AegisBorn"
Type="AegisBorn.AegisBornApplication"
EnableAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
With all this in place, you can build your project and go to the Photon Control and start the server. Thats all we need to do server side.
No comments:
Post a Comment