How it works?

This page will explain how the Unity EdgeMultiplay solution works, If you want information about how to add EdgeMultiplay to your Unity project, please check the Configuration section.

EdgeMultiplay is Unity Multiplayer Solution, EdgeMultiplay client uses Websockets & UDP Messages to communicate with a NodeJS Server that uses cluster module for scalability.

to use EdgeMultiplay in Unity you need to have a GameManager & a PlayerManager
You can find a template of both in Assets/Create/EdgeMultiplay

Let's start with the GameManager these are the requirements for your GameManager :

  • inherits for EdgeMultiplayCallbacks
  • Add EdgeMultiplay namespace
  • Requires EdgeManager Component Similar to this
    using UnityEngine;
    [RequireComponent(typeof(EdgeManager))]
    public class GameManager : EdgeMultiplayCallbacks {

In your GameManager you can start the Connection to the server by using ConnectToEdge()

void Start () {
ConnectToEdge();
}

In your GameManager you can access all of the EdgeMultiplay callbacks, A callback is a function that is to be executed after another function has finished executing

See full list of EdgeMultiplayCallbacks here

Once the player is registered on the server, It is the best time to call CreateRoom() or JoinRoom() or JoinOrCreateRoom()

// Called once the server registers the player right after the connection is established
public override void OnRegisterEvent(){
print ("Game Session received from server");
EdgeManager.JoinOrCreateRoom(playerName: "John Doe", playerAvatar: 0, maxPlayersPerRoom: 2);
}

Once the Game Starts, EdgeManager will spawn players in the specified positions in EdgeManager.SpawnInfo

Players can send GamePlay Events using EdgeManager.BroadcastMessage()

// send message to all other players
// set the eventName to a unique name to differniate between your events OnMessageReceived()
EdgeManager.MessageSender.BroadcastMessage(new GamePlayEvent()
{
eventName = "Score",
floatData = new float[] { 1, 1 }
});

If a player received a message it will be available in OnMessageReceived()

// Called once a GamePlay Event is received from the server
public override void OnMessageReceived(GamePlayEvent gamePlayEvent){
print ("GamePlayEvent received from server, event name: " + gamePlayEvent.eventName );
switch (gamePlayEvent.eventName)
{
case "Shooting":
print("Shooting");
break;
case "Score":
//DoStuff();
break;
case "Collision":
print("Collision");
break;
}
}

If you want to sync a player position and/or rotation add EdgeMultiplayObserver, Syncing Objects is done using UDP messages.

EdgeMultiplay
Definition: DataContracts.cs:29