Server - 0F00/netwIO GitHub Wiki

Introduction to the Server :

MasterServer :

The Master Server handle the Questions from the Clients, like Create/Join/CreateJoinRoom. The Client connect to the Master Server with a WebSocket Connection, first at all before a Client can do anything he must authenticate him. Then the Client can say that he wanna try to "CreateJoinRoom". The MasterServer search in his List if any Server spawned with the given Context.

If their isnt any GameServer, the Master Server will spawn a new Instance of the RoomType with the given RoomName. And if the GameServer is correctly spawned, the Master Server will let the Client know on which Port the GameServer listen.

Construction of a GameServer Class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
 
namespace netwIO.Server
{
    //GlobalSettigs
    //ConnectionString is the LoginData to the MySql Server.
    public static class GlobalSettings
    {
        public static string ConnectionString = "Server=xxx.xxx.xxx.xx; database=xxxxxxxx; UID=xxxxxx; password=xxxxxxxxxx";
    }
    //Here comes ur Variable from the Player.
    //Like Coins, Credits, Level
    //Every Player has his own Instance of this Class.
    public class Player : BasePlayer
    {
        public int Level = 0;
    }
    //This an Instance of the GameServer.
    //The Class Name is the RoomType
    //This Class will be created when a Player create a Room with this Type.
    [Settings(Protocol = ProtocolType.Tcp)]
    public class GameCode : BaseClass<Player>
    {
        //Here you can List all your Players
        public List<Player> Players = new List<Player>();
 
        //Will be invoked when the a Instance of this Type is created.
        public override void GameStarted()
        {
            Debug.Log("Game Started. Instance 1");
        }
 
        //Dont Work at this Time.
        public override void GameClosed()
        {
            Debug.Log("Game Closed.");
        }
        //Will be invoked when a Player Joins.
        public override void UserJoined(Player player)
        {
            Players.Add(player);
            Debug.Log(player.Level);
            Debug.Log("Player Joined : " + player.Username);
        }
 
        //This is invoked when a Player lefts the Game.
        public override void UserLeft(Player player)
        {
            Debug.Log("User Left : " + player.Username);
            Players.Remove(player);
        }
        //This is invoked when a Player sends a Message to the Server.
        public override void OnMessage(Player player, Message m)
        {
            switch (m.Type)
            {
                case "LoginPlayer":
                    Message msg = Message.Create("OnMove");
                    msg.Add("pos", new Vector3(1, 2, 3));
                    return;
            }
            Debug.Log("MessageType : " + m.Type);
            Debug.LogWarning("Unknown Message from a Player. This occurs when a MessageType is not implemented.");
        }
    }
}

Inherited Methods and Functions from BaseClass

public override void OnGameStarted()

This Method is invoked on Game starts.

public override void OnGameClosed()

This Method is invoked when the Game stops.

public override void OnPlayerJoined(Player player)

This Method is invoked when a Players joins.

public override void OnPlayerLeft(Player player)

This Method is invoked when a Player left the Game.

BasePlayer :

Properties :

Player.Username;

The Username from the connected User.

Player.UserId;

The UserId from the connected User. (If the User isn't loggedIn this UserId is random generated.)

Functions :

player.Send(Message);

This Function will Send a Message to the Player.

⚠️ **GitHub.com Fallback** ⚠️