Skip to content

Networking

Rainer Koschke edited this page Jan 31, 2024 · 9 revisions

Networking

Network

Networking can be enabled by attaching the SEE.Net.Network script to a GameObject and configuring as needed. Both the client and potentially the server are initialized through this script. This script also handles the voice server and the sending of packets.

Server and Client

The SEE.Net.Server and the SEE.Net.Client connect to each other and transfer every incoming SEE.Net.Packet to a SEE.Net.PacketHandler, which handles those packets according to their type.

Actions

Actions are commands that can contain data and can be broadcasted by every client to a list of clients. First, every action is sent to the server, where SEE.Net.AbstractAction.ExecuteOnServer() is executed. Then, the server sends the action to the desired clients, where SEE.Net.AbstractAction.ExecuteOnClient() is executed. Note that therefore every action is always executed on the server and on at least one client!

Overview

The following actions are executed as a new action is instantiated and executed:

AbstractAction a = new MyAction(); a.Execute(ReceivingClients) → Serialization on client → Send to server → Deserialized on server → Call a.ExecuteOnServer() on server → Serialization on server → Send to ReceivingClients → Deserialize on client → Call a.ExecuteOnClient() on client

Creation

Networked actions can be created by inheriting from SEE.Net.AbstractAction and overriding the necessary methods. Note that actions use UnityEngine.JsonUtility for (de)serialization, which limits the way action-classes can be structured. Specific limitations can be found here.

Execution

Actions are executed by calling SEE.Net.AbstractAction.Execute(System.Net.IPEndPoint[]) on the action and passing a list of the recipients (everyone if null). If a player hosts a server, it is ensured that SEE.Net.AbstractAction.ExecuteOnServer() is always called before SEE.Net.AbstractAction.ExecuteOnClient().

Example

Note that the str is public, so that is can be (de)serialized and thus read by every client. The server does not have to do anything in this example, so it just returns true to indicate that no error occurred. The client checks whether it is the sender and if not, prints the received message.

public class HelloWorldAction : SEE.Net.AbstractAction
{
    public string str;

    public HelloWorldAction()
    {
        str = "Hello World!";
    }

    protected override void ExecuteOnServer()
    {
    }

    protected override void ExecuteOnClient()
    {
        if (!IsRequester())
        {
            Debug.Log(str);
        }
    }

    [...]
}

This action can be created, sent to the server and every client and executed by calling:

new HelloWorldAction().Execute(null);

Dependencies

Clone this wiki locally