Mirror
Mirror on Edgegap
This guide will help you create a headless server on Edgegap for a Unity project using Mirror as its networking solution.
This guide will use the open-source sample project Tanks, which is already available in your Mirror sample, location Assets/Mirror/Examples/Tanks.
The final sample can be found on our GitHub.
Build the game server
Once ready with your game, head to the Build screen of the Unity Editor, under File -> Build Settings in the top menus. Make sure to select the right presets depending on your version of Unity.
Prior to version 2021.2:
Set
Target PlatformtoLinux;Set
Architecturetox86_64;Check the
Server Buildoption.
Otherwise:
Set
PlatformtoDedicated Server;Set
Target PlatformtoLinux.
Then press build and select a new empty folder named linux_server as the file destination. Transfer linux_server folder to a second empty folder, which will be refered as the [SERVER BUILD] folder in this document.
Containerizing the dedicated game server
We will create a docker image containing the dedicated game server in this part. You might also be interested in reading Unity Server in Docker.
If you need more informations about Docker with Edgegap, please refer to this documentation.
Dockerfile
Take note of the port you use for network communications, referred as the [GAME PORT]. By default, the port used is 7777. You can find this information in the Unity Editor, on the NetworkManager game object, in the Transport component.
Copy the above lines and paste them in your Dockerfile, placed inside
[SERVER BUILD]. Modify the[GAME PORT]placeholders to your game port.
Having the [GAME PORT] opened on both TCP and UDP allow you to use any transport you prefer in the NetworkManager Mirror component. Finally, create a file named boot.sh at the root of the [SERVER BUILD] folder. It will be executed when starting the image in a container.
Copy the following two lines, make sure to replace the
[YOUR GAME]placeholders with the name of the generated file.
At this point, you should have the following hierarchy:
[SERVER BUILD] folder> > -Dockerfile> > -boot.sh> > -linux_serverfolder > > > - Unity generated files
Start a command prompt in the
[SERVER BUILD]folder, and run the following Docker commands:
For ARM CPU (Mac M1, M2, etc.) users, see the dedicated page.
Using Linux
Using cmd
Using Powershell
After these commands, you should be able to see your uploaded image on the Edgegap website if you are using the Edgegap Container Registry. See this doc if you want to use the Edgegap registry. You can also use another private registry.
Deploying to Edgegap
Navigate to the Applications & Games page of the website. Click on the Create New button in the top right hand corner to access the application form. Here are the fields and how to fill them properly:
Application name : Can be any notable name you want to use to easily recognize your application among others.
Image : Can be any specific image you want to use to easily recognize your application among others.
Version name : You may want to use a version name to describe the scope of the version you are deploying. Examples may be โdemoโ, โproductionโ, โv1โ, โv2โ
Container :
Registry : โ[URL]โ, where [URL] is the value from the credentials you can display on the Container Repository page.
Image repository : โ[PROJECT]/[YOUR GAME]โ, where [PROJECT] and [YOUR GAME] are the values you used earlier when pushing the docker image.
Tag : โ[TAG]โ, where [TAG] is the value you used earlier when pushing the docker image.
Tick โUsing a private repositoryโ
Private registry username : โ[USERNAME]โ, where [USERNAME] is the value from your credentials.
Private registry token : โ[TOKEN]โ, where [TOKEN] is the value from your credentials.
Requirements : Left as is.
Ports :
Click the
+ Add portlink to add a new port, and add the following entries :[GAME PORT]-TCP/UDP- disable Verifications3389 - TCP - disable Verifications
Once your application has been created, you can press the Deploy button to proceed with deploying your game server. Choose the region you want to deploy in, and enter the number of random players you want to generate depending on your game. Check that everything is running smoothly by verifying the following:
Latest Status should be set to
Ready.In the
Port Mappingtab, you should be seeing the port you set in the application creation form:

Add sample HUD in your client application
Set the
Portvalue of theTransportcomponent of theNetworkManagerto the external port defined in thePort Mappingtab of your deployment.
In this example, the port was set to 31887. This primarily depends on the game you are developing and will most likely be set programmatically in the gameโs codebase.
Set the value of
Network Addressof theNetwork Managerto your deployment'sHost. This URL can be found in theDeployment Summaryon the dashboard or with the API.
In this example, the address was set to 0ace560706a5.pr.edgegap.net. Again, this value will most likely be set programmatically during the clientโs communication with the master server/API responsible for the matchmaking process.
With the correct information, you should be able to connect to the game server normally and be able to start playing right away.
You now have a Mirror project available to deploy on demand!
With seat-based deployments, it's possible to use Mirror to create a system that automatically removes hanging Edgegap sessions once a player disconnects from the server, using NetworkManager callback functions, and a NetworkBehaviour script attached to the player prefab that uses a Remote Procedure Call (RPC) function and a Command function.
When the server starts, the NetworkManager retrieves the list of session IDs linked to its deployment from the Edgegap API and stores it. Afterwards, when a new player connects to the server, a client-side function is initiated via RPC that will send the player's IP address back to the server with a command. With the player's IP, the server checks for a matching IP in each session's data; The server gets the session data using its cached ID with the Edgegap API. If a match is found, the session ID is mapped to that player's NetworkConnectionToClient.
Since new sessions can be added after the server starts, the list of session IDs is updated and the new sessions are checked if a match can't be found the first time around.
Finally, once a player disconnects from the server, the server uses that player's NetworkConnectionToClient to retrieve their associated session ID, then uses the Edgegap API to delete that session. This frees a socket in the deployment for a new player to join.
PlayerNetworkBehaviour
CustomNetworkManager
Optional features
This script can be expanded upon with optional, independent features that help manage the sessions in specific cases.
For example, a timeout can be set to remove empty seat sessions after a configurable amount of time following full server initialization. This is a useful feature in the case of a player that quits before their matchmaker ticket gets resolved, which would create an empty session once a match is found. If the player never sends their IP address to be mapped to the session ID before the timeout resolves, then that session gets deleted to free up a socket.
Another feature can be to disconnect inactive players after a prolonged period of time in order to free up sockets. Once connected to the server, the client needs to send some minimalistic heartbeat message to the server every few seconds, or otherwise gets disconnected if too many heartbeats are missed in a row. Both the amount of time between heartbeats and the maximum number of messages that can be missed in a row can be configured, depending on the game's needs. In this sample, a heartbeat gets sent every few seconds if the tank is moving around the map or shooting.
PlayerNetworkBehaviour - Seat Session Management
CustomNetworkManager - Seat Session Management
Last updated
Was this helpful?

