This is a tutorial for advanced developers. If you have just started to use Photon, you can safely skip this.
If you are developing a complex game, you probably want to distribute functionality between different servers. You can use Photon's Server-To-Server (S2S) features to implement the communication between your Photon servers.
S2S-communication in Photon is very similar to client-to-server-communication. One Photon instance initiates a connection (this is the "outbound" side of the connection) to the target Photon server (this is the "inbound" side of the connection). On each side, a peer is created and can be used to send operations / events over the S2S connection.
The outbound side
Let's assume you have an application "MyOutboundApplication", which inherits from
To initiate the S2S-connection, you need to call one of the
It is recommended to use TCP connections for S2S communication.
This code is needed to set up the S2S connection:
The above code establishes the S2S connection during
You can also do this later in your codebase.
However, you should use only one connection between any two servers - keep the connection open and use that one connection to send all data through it.
To send data to your target server, you simply call the SendOperation method of that peer, like this:
The implementation of the "OutboundPeer" class is quite simple as well. This is a minimal sample, add your own code as you see fit:
If the connection is established successfully,
OnConnectionEstablished method will be triggered.
OnConnectionFailed() method is called.
You might want to override that method and add some error handling, logging etc. there.
This is all that needs to be done on the "outbound" side of the S2S connection.
The "inbound" side
Now that the "outbound" side is done, we need to implement the "inbound" side of the S2S connection.
First, you need to create a peer object. To determine the incoming connection source ("client" or "server"), it is recommended to listen to different ports.
Let's assume that you have an application that inherits from
ApplicationBase. Add this code:
The MyInboundPeer is just a simple class that inherits of the PeerBase class:
This is all that needs to be done on the "inbound" side of the S2S connection.
If you have decided to use a separate port to listen for S2S connections on the inbound side, you need to define a new listener in the PhotonServer.config, like this: