Master server
This script and scene is created to using multiple MultipleServer
. The config is a json file in its root folder (with the exe).
{
"Port": 5881,
"Password": "***",
"FPS": 60,
"RefreshInterval": 120,
"MaxInstanceNb": 50,
"WaitingTimeout": 10,
"ServersPsw": "MY STRONG PASSWORD",
"ServersPort": 5882,
"Servers": [
"localhost"
]
}
- Port: Master server port (client connection)
- Password: Master server password (client connection)
- RefreshInterval: Interval of the hot refresh of the configuration file
- MaxInstanceNb: Limit the number of instances on the
MultipleServer
. - WaitingTimeout: When a client is in the waiting list, if it does not call against in this interval, this party will be removed from the waiting list
- ServersPsw: Password of
MultipleServer
s. - ServersPort:
MultipleServer
s port. - Servers:
MultipleServer
address list.
Create a new game
Using SimpleNetMasterCmds.NewGame
without arguments, the master server returns to a new unique ID (string). Save it on the client machine or on the client account.
Join a game
Using SimpleNetMasterCmds.JoinGame
with the game ID as argument, the master server will try to create a new room on a MultipleServer
and returns [true, address, port, password].
The game ID length must be > 10
If all servers are full, the game will enter in a waiting loop. Master returns [false, position (int)]. Call this in a loop (with an interval of x seconds, this must be < WaitingTimeout
) to create a wait routine.
If the game is already online, the master server returns [true, address, port, password].
Client example
using System.Threading.Tasks;
using EODE.Wonderland;
using EODE.Wonderland.Networking;
using EODE.Wonderland.SimpleNet;
using UnityEngine;
public class SimpleNetTestConnect : MonoBehaviour {
public string GameId = "";
public SceneField AutoLoadScene;
Cloud _cloud;
Peer _master;
async void Start() {
_cloud = new Cloud();
_master = await _cloud.CreatePeer("localhost", 5881, "***");
if (_master.IsConnected) {
if (string.IsNullOrEmpty(GameId)) {
Debug.Log("Asking for a new GameId");
var response = await _master.Send(SimpleNetMasterCmds.NewGame, _cloud.DefaultNetwork);
GameId = response.ReadPrimitive<string>();
if (!string.IsNullOrEmpty(GameId)) {
await JoinGame();
}
else {
Debug.LogErr("Cannot create a new GameId");
}
}
else {
await JoinGame();
}
}
else {
if (_master.ConnectionException != null) {
Debug.LogErr("Master server is offline");
}
else {
Debug.LogErr("Cannot connect to master");
}
}
}
void Update() {
if (_cloud != null) _cloud.Update();
}
async void OnDestroy() {
await _cloud.Close();
}
async Task JoinGame() {
var response = await _master.Send(SimpleNetMasterCmds.JoinGame, GameId, _cloud.DefaultNetwork);
bool success = response.ReadPrimitive<bool>();
if (success) {
var address = response.ReadPrimitive<string>();
var port = response.ReadPrimitive<int>();
var password = response.ReadPrimitive<string>();
if (string.IsNullOrEmpty(address) || port <= 0 || string.IsNullOrEmpty(password)) {
Debug.LogErr("Request error");
}
else {
var go = new GameObject("NetManager");
var nm = go.AddComponent<NetManager>();
nm.Address = address;
nm.Port = port;
nm.Password = password;
nm.AutoConnect = true;
Debug.Log("Connecting...");
var timeout = Time.time + 5f;
await new WaitWhile(() => !NetManager.IsReady && timeout > Time.time);
if (!NetManager.IsReady) {
Debug.LogErr("Cannot connect to GameId server");
}
else {
Debug.Log("Connected");
if (AutoLoadScene.BundledAsset.IsValid) {
Debug.Log("Start scene " + AutoLoadScene.Path);
await AutoLoadScene.Load();
}
}
}
}
else {
int nb = response.ReadPrimitive<int>();
if (nb < 0) {
Debug.LogErr("Cannot join this game");
}
// join immediatly
else if (nb == 0) {
Debug.Log("Waiting end : " + nb);
await new WaitForSeconds(1f);
JoinGame().WrapErrors();
}
// loop
else {
Debug.Log("Waiting list : " + nb);
await new WaitForSeconds(5f);
JoinGame().WrapErrors();
}
}
}
}