Skip to content

Mirror::NetworkServer

The NetworkServer. More...

Public Functions

Name
void Shutdown()
This shuts down the server and disconnects all clients.
void Listen(int maxConns)
Start the server, setting the maximum number of connections.
bool AddConnection(NetworkConnectionToClient conn)
bool RemoveConnection(int connectionId)
This removes an external connection added with AddExternalConnection().
void ActivateHostScene()
void SendToAll< T >(T msg, int channelId =Channels.DefaultReliable, bool sendToReadyOnly =false)
Send a message to all connected clients, both ready and not-ready.
void SendToReady< T >(T msg, int channelId =Channels.DefaultReliable)
Send a message to only clients which are ready.
void SendToReady< T >(NetworkIdentity identity, T msg, bool includeOwner =true, int channelId =Channels.DefaultReliable)
Send a message to only clients which are ready with option to include the owner of the object identity.
void SendToReady< T >(NetworkIdentity identity, T msg, int channelId)
Send a message to only clients which are ready including the owner of the object identity.
void DisconnectAll()
Disconnect all currently connected clients, including the local connection.
void DisconnectAllConnections()
Disconnect all currently connected clients except the local connection.
bool NoConnections()
If connections is empty or if only has host
void Update()
Called from NetworkManager in LateUpdate
void RegisterHandler< T >(Action< NetworkConnection, T > handler, bool requireAuthentication =true)
Register a handler for a particular message type.
void RegisterHandler< T >(Action< T > handler, bool requireAuthentication =true)
Register a handler for a particular message type.
void ReplaceHandler< T >(Action< NetworkConnection, T > handler, bool requireAuthentication =true)
Replaces a handler for a particular message type.
void ReplaceHandler< T >(Action< T > handler, bool requireAuthentication =true)
Replaces a handler for a particular message type.
void UnregisterHandler< T >()
Unregisters a handler for a particular message type.
void ClearHandlers()
Clear all registered callback handlers.
void SendToClientOfPlayer< T >(NetworkIdentity identity, T msg, int channelId =Channels.DefaultReliable)
send this message to the player only
bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId, bool keepAuthority =false)
This replaces the player object for a connection with a different player object. The old player object is not destroyed.
bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, bool keepAuthority =false)
This replaces the player object for a connection with a different player object. The old player object is not destroyed.
bool AddPlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId)
bool AddPlayerForConnection(NetworkConnection conn, GameObject player)
void SetClientReady(NetworkConnection conn)
Sets the client to be ready.
void SetAllClientsNotReady()
Marks all connected clients as no longer ready.
void SetClientNotReady(NetworkConnection conn)
Sets the client of the connection to be not-ready.
void RemovePlayerForConnection(NetworkConnection conn, bool destroyServerObject)
Removes the player object from the connection
void DestroyPlayerForConnection(NetworkConnection conn)
This destroys all the player objects associated with a NetworkConnections on a server.
void Spawn(GameObject obj, NetworkConnection ownerConnection =null)
Spawn the given game object on all clients which are ready.
void Spawn(GameObject obj, GameObject ownerPlayer)
This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.
void Spawn(GameObject obj, Guid assetId, NetworkConnection ownerConnection =null)
This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.
void Destroy(GameObject obj)
Destroys this object and corresponding objects on all clients.
void UnSpawn(GameObject obj)
This takes an object that has been spawned and un-spawns it.
bool SpawnObjects()
This causes NetworkIdentity objects in a scene to be spawned on a server.

Public Properties

Name
NetworkConnectionToClient localConnection
The connection to the host mode client (if any).
bool active

Public Attributes

Name
bool localClientActive
Dictionary< int, NetworkConnectionToClient > connections
A list of local connections on the server.
bool dontListen
bool disconnectInactiveConnections
Should the server disconnect remote connections that have gone silent for more than Server Idle Timeout?
float disconnectInactiveTimeout
Timeout in seconds since last message from a client after which server will auto-disconnect.

Detailed Description

class Mirror::NetworkServer;

The NetworkServer.

NetworkServer handles remote connections from remote clients via a NetworkServerSimple instance, and also has a local connection for a local client.

The NetworkServer is a singleton. It has static convenience functions such as NetworkServer.SendToAll() and NetworkServer.Spawn() which automatically use the singleton instance.

The NetworkManager uses the NetworkServer, but it can be used without the NetworkManager.

The set of networked objects that have been spawned is managed by NetworkServer. Objects are spawned with NetworkServer.Spawn() which adds them to this set, and makes them be created on clients. Spawned objects are removed automatically when they are destroyed, or than they can be removed from the spawned set by calling NetworkServer.UnSpawn() - this does not destroy the object.

There are a number of internal messages used by NetworkServer, these are setup when NetworkServer.Listen() is called.

Public Functions Documentation

function Shutdown

static inline void Shutdown()

This shuts down the server and disconnects all clients.

function Listen

static inline void Listen(
    int maxConns
)

Start the server, setting the maximum number of connections.

Parameters:

  • maxConns Maximum number of allowed connections

function AddConnection

static inline bool AddConnection(
    NetworkConnectionToClient conn
)

Parameters:

  • conn Network connection to add.

Return: True if added.

This accepts a network connection and adds it to the server.

This connection will use the callbacks registered with the server.

function RemoveConnection

static inline bool RemoveConnection(
    int connectionId
)

This removes an external connection added with AddExternalConnection().

Parameters:

  • connectionId The id of the connection to remove.

Return: True if the removal succeeded

function ActivateHostScene

static inline void ActivateHostScene()

function SendToAll< T >

static inline void SendToAll< T >(
    T msg,
    int channelId =Channels.DefaultReliable,
    bool sendToReadyOnly =false
)

Send a message to all connected clients, both ready and not-ready.

Parameters:

  • msg Message
  • channelId Transport channel to use
  • sendToReadyOnly Indicates if only ready clients should receive the message

Template Parameters:

  • T Message type

See NetworkConnection.isReady

function SendToReady< T >

static inline void SendToReady< T >(
    T msg,
    int channelId =Channels.DefaultReliable
)

Send a message to only clients which are ready.

Parameters:

  • msg Message
  • channelId Transport channel to use

Template Parameters:

  • T Message type.

See NetworkConnection.isReady

function SendToReady< T >

static inline void SendToReady< T >(
    NetworkIdentity identity,
    T msg,
    bool includeOwner =true,
    int channelId =Channels.DefaultReliable
)

Send a message to only clients which are ready with option to include the owner of the object identity.

Parameters:

  • identity Identity of the owner
  • msg Message
  • includeOwner Should the owner of the object be included
  • channelId Transport channel to use

Template Parameters:

  • T Message type.

See NetworkConnection.isReady

function SendToReady< T >

static inline void SendToReady< T >(
    NetworkIdentity identity,
    T msg,
    int channelId
)

Send a message to only clients which are ready including the owner of the object identity.

Parameters:

  • identity identity of the object
  • msg Message
  • channelId Transport channel to use

Template Parameters:

  • T Message type

See NetworkConnection.isReady

function DisconnectAll

static inline void DisconnectAll()

Disconnect all currently connected clients, including the local connection.

This can only be called on the server. Clients will receive the Disconnect message.

function DisconnectAllConnections

static inline void DisconnectAllConnections()

Disconnect all currently connected clients except the local connection.

This can only be called on the server. Clients will receive the Disconnect message.

function NoConnections

static inline bool NoConnections()

If connections is empty or if only has host

Return:

function Update

static inline void Update()

Called from NetworkManager in LateUpdate

The user should never need to pump the update loop manually

function RegisterHandler< T >

static inline void RegisterHandler< T >(
    Action< NetworkConnection, T > handler,
    bool requireAuthentication =true
)

Register a handler for a particular message type.

Parameters:

  • handler Function handler which will be invoked when this message type is received.
  • requireAuthentication True if the message requires an authenticated connection

Template Parameters:

  • T Message type

There are several system message types which you can add handlers for. You can also add your own message types.

function RegisterHandler< T >

static inline void RegisterHandler< T >(
    Action< T > handler,
    bool requireAuthentication =true
)

Register a handler for a particular message type.

Parameters:

  • handler Function handler which will be invoked when this message type is received.
  • requireAuthentication True if the message requires an authenticated connection

Template Parameters:

  • T Message type

There are several system message types which you can add handlers for. You can also add your own message types.

function ReplaceHandler< T >

static inline void ReplaceHandler< T >(
    Action< NetworkConnection, T > handler,
    bool requireAuthentication =true
)

Replaces a handler for a particular message type.

Parameters:

  • handler Function handler which will be invoked when this message type is received.
  • requireAuthentication True if the message requires an authenticated connection

Template Parameters:

  • T Message type

See also [RegisterHandler(T)(Action(NetworkConnection, T), bool)]

function ReplaceHandler< T >

static inline void ReplaceHandler< T >(
    Action< T > handler,
    bool requireAuthentication =true
)

Replaces a handler for a particular message type.

Parameters:

  • handler Function handler which will be invoked when this message type is received.
  • requireAuthentication True if the message requires an authenticated connection

Template Parameters:

  • T Message type

See also [RegisterHandler(T)(Action(NetworkConnection, T), bool)]

function UnregisterHandler< T >

static inline void UnregisterHandler< T >()

Unregisters a handler for a particular message type.

Template Parameters:

  • T Message type

function ClearHandlers

static inline void ClearHandlers()

Clear all registered callback handlers.

function SendToClientOfPlayer< T >

static inline void SendToClientOfPlayer< T >(
    NetworkIdentity identity,
    T msg,
    int channelId =Channels.DefaultReliable
)

send this message to the player only

Parameters:

  • identity
  • msg

Template Parameters:

  • T Message type

function ReplacePlayerForConnection

static inline bool ReplacePlayerForConnection(
    NetworkConnection conn,
    GameObject player,
    Guid assetId,
    bool keepAuthority =false
)

This replaces the player object for a connection with a different player object. The old player object is not destroyed.

Parameters:

  • conn Connection which is adding the player.
  • player Player object spawned for the player.
  • assetId
  • keepAuthority Does the previous player remain attached to this connection?

Return: True if connection was successfully replaced for player.

If a connection already has a player object, this can be used to replace that object with a different player object. This does NOT change the ready state of the connection, so it can safely be used while changing scenes.

function ReplacePlayerForConnection

static inline bool ReplacePlayerForConnection(
    NetworkConnection conn,
    GameObject player,
    bool keepAuthority =false
)

This replaces the player object for a connection with a different player object. The old player object is not destroyed.

Parameters:

  • conn Connection which is adding the player.
  • player Player object spawned for the player.
  • keepAuthority Does the previous player remain attached to this connection?

Return: True if connection was successfully replaced for player.

If a connection already has a player object, this can be used to replace that object with a different player object. This does NOT change the ready state of the connection, so it can safely be used while changing scenes.

function AddPlayerForConnection

static inline bool AddPlayerForConnection(
    NetworkConnection conn,
    GameObject player,
    Guid assetId
)

Parameters:

  • conn Connection which is adding the player.
  • player Player object spawned for the player.
  • assetId

Return: True if connection was sucessfully added for a connection.

When an AddPlayer message handler has received a request from a player, the server calls this to associate the player object with the connection.

When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail.

function AddPlayerForConnection

static inline bool AddPlayerForConnection(
    NetworkConnection conn,
    GameObject player
)

Parameters:

  • conn Connection which is adding the player.
  • player Player object spawned for the player.

Return: True if connection was successfully added for a connection.

When an AddPlayer message handler has received a request from a player, the server calls this to associate the player object with the connection.

When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail.

function SetClientReady

static inline void SetClientReady(
    NetworkConnection conn
)

Sets the client to be ready.

Parameters:

  • conn The connection of the client to make ready.

When a client has signaled that it is ready, this method tells the server that the client is ready to receive spawned objects and state synchronization updates. This is usually called in a handler for the SYSTEM_READY message. If there is not specific action a game needs to take for this message, relying on the default ready handler function is probably fine, so this call wont be needed.

function SetAllClientsNotReady

static inline void SetAllClientsNotReady()

Marks all connected clients as no longer ready.

All clients will no longer be sent state synchronization updates. The player's clients can call ClientManager.Ready() again to re-enter the ready state. This is useful when switching scenes.

function SetClientNotReady

static inline void SetClientNotReady(
    NetworkConnection conn
)

Sets the client of the connection to be not-ready.

Parameters:

  • conn The connection of the client to make not ready.

Clients that are not ready do not receive spawned objects or state synchronization updates. They client can be made ready again by calling SetClientReady().

function RemovePlayerForConnection

static inline void RemovePlayerForConnection(
    NetworkConnection conn,
    bool destroyServerObject
)

Removes the player object from the connection

Parameters:

  • conn The connection of the client to remove from
  • destroyServerObject Indicates whether the server object should be destroyed

function DestroyPlayerForConnection

static inline void DestroyPlayerForConnection(
    NetworkConnection conn
)

This destroys all the player objects associated with a NetworkConnections on a server.

Parameters:

  • conn The connections object to clean up for.

This is used when a client disconnects, to remove the players for that client. This also destroys non-player objects that have client authority set for this connection.

function Spawn

static inline void Spawn(
    GameObject obj,
    NetworkConnection ownerConnection =null
)

Spawn the given game object on all clients which are ready.

Parameters:

  • obj Game object with NetworkIdentity to spawn.
  • ownerConnection The connection that has authority over the object

This will cause a new object to be instantiated from the registered prefab, or from a custom spawn function.

function Spawn

static inline void Spawn(
    GameObject obj,
    GameObject ownerPlayer
)

This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.

Parameters:

  • obj The object to spawn.
  • ownerPlayer The player object to set Client Authority to.

This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.

function Spawn

static inline void Spawn(
    GameObject obj,
    Guid assetId,
    NetworkConnection ownerConnection =null
)

This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.

Parameters:

  • obj The object to spawn.
  • assetId The assetId of the object to spawn. Used for custom spawn handlers.
  • ownerConnection The connection that has authority over the object

This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.

function Destroy

static inline void Destroy(
    GameObject obj
)

Destroys this object and corresponding objects on all clients.

Parameters:

  • obj Game object to destroy.

In some cases it is useful to remove an object but not delete it on the server. For that, use NetworkServer.UnSpawn() instead of NetworkServer.Destroy().

function UnSpawn

static inline void UnSpawn(
    GameObject obj
)

This takes an object that has been spawned and un-spawns it.

Parameters:

  • obj The spawned object to be unspawned.

The object will be removed from clients that it was spawned on, or the custom spawn handler function on the client will be called for the object.

Unlike when calling NetworkServer.Destroy(), on the server the object will NOT be destroyed. This allows the server to re-use the object, even spawn it again later.

function SpawnObjects

static inline bool SpawnObjects()

This causes NetworkIdentity objects in a scene to be spawned on a server.

Return: Success if objects where spawned.

NetworkIdentity objects in a scene are disabled by default. Calling SpawnObjects() causes these scene objects to be enabled and spawned. It is like calling NetworkServer.Spawn() for each of them.

Public Property Documentation

property localConnection

static NetworkConnectionToClient localConnection;

The connection to the host mode client (if any).

property active

static bool active;

Checks if the server has been started.

This will be true after NetworkServer.Listen() has been called.

Public Attributes Documentation

variable localClientActive

static bool localClientActive => localConnection != null;

True is a local client is currently active on the server.

This will be true for "Hosts" on hosted server games.

variable connections

static Dictionary< int, NetworkConnectionToClient > connections = new Dictionary<int, [NetworkConnectionToClient](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkConnectionToClient/)>();

A list of local connections on the server.

variable dontListen

static bool dontListen;

If you enable this, the server will not listen for incoming connections on the regular network port.

This can be used if the game is running in host mode and does not want external players to be able to connect - making it like a single-player game. Also this can be useful when using AddExternalConnection().

variable disconnectInactiveConnections

static bool disconnectInactiveConnections;

Should the server disconnect remote connections that have gone silent for more than Server Idle Timeout?

This value is initially set from NetworkManager in SetupServer and can be changed at runtime

variable disconnectInactiveTimeout

static float disconnectInactiveTimeout = 60f;

Timeout in seconds since last message from a client after which server will auto-disconnect.

This value is initially set from NetworkManager in SetupServer and can be changed at runtime

By default, clients send at least a Ping message every 2 seconds.

The Host client is immune from idle timeout disconnection.

Default value is 60 seconds.


Updated on 26 January 2021 at 15:30:26 UTC