in GameLiftExampleUnityProject/Assets/Scripts/Server/GameLift.cs [51:129]
public void Awake()
{
//Initiate the simple statsD client
this.statsdClient = new SimpleStatsdClient("localhost", 8125);
//Get the port from command line args
listeningPort = this.GetPortFromArgs();
System.Console.WriteLine("Will be running in port: " + this.listeningPort);
//InitSDK establishes a local connection with the Amazon GameLift agent to enable
//further communication.
var initSDKOutcome = GameLiftServerAPI.InitSDK();
if (initSDKOutcome.Success)
{
ProcessParameters processParameters = new ProcessParameters(
(gameSession) => {
//Respond to new game session activation request. GameLift sends activation request
//to the game server along with a game session object containing game properties
//and other settings.
// Activate the session
GameLiftServerAPI.ActivateGameSession();
//Start waiting for players
this.gameSessionInfoReceived = true;
this.gameSessionId = gameSession.GameSessionId;
//Set the game session tag (CloudWatch dimension) for custom metrics
string justSessionId = this.gameSessionId.Split('/')[2];
this.statsdClient.SetCommonTagString("#gamesession:" + justSessionId);
//Send session started to CloudWatch just for testing
this.statsdClient.SendCounter("game.SessionStarted", 1);
},
() => {
//OnProcessTerminate callback. GameLift invokes this callback before shutting down
//an instance hosting this game server. It gives this game server a chance to save
//its state, communicate with services, etc., before being shut down.
//In this case, we simply tell GameLift we are indeed going to shut down.
GameLiftServerAPI.ProcessEnding();
Application.Quit();
},
() => {
//This is the HealthCheck callback.
//GameLift invokes this callback every 60 seconds or so.
//Here, a game server might want to check the health of dependencies and such.
//Simply return true if healthy, false otherwise.
//The game server has 60 seconds to respond with its health status.
//GameLift will default to 'false' if the game server doesn't respond in time.
//In this case, we're always healthy!
return true;
},
//Here, the game server tells GameLift what port it is listening on for incoming player
//connections. We will use the port received from command line arguments
listeningPort,
new LogParameters(new List<string>()
{
//Let GameLift know where our logs are stored. We are expecting the command line args to specify the server with the port in log file
"/local/game/logs/myserver"+listeningPort+".log"
}));
//Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions
var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
if (processReadyOutcome.Success)
{
print("ProcessReady success.");
}
else
{
print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
}
}
else
{
print("InitSDK failure : " + initSDKOutcome.Error.ToString());
}
}