Skip to content

Mirror::NetworkClient

This is a network client class used by the networking system. It contains a NetworkConnection that is used to connect to a network server. More...

Public Functions

Name
void Connect(string address)
Connect client to a NetworkServer instance.
void Connect(Uri uri)
Connect client to a NetworkServer instance.
void ConnectHost()
void ConnectLocalServer()
connect host mode
void DisconnectLocalServer()
disconnect host mode. this is needed to call DisconnectMessage for the host client too.
void Disconnect()
Disconnect from server.
void Send< T >(T message, int channelId =Channels.DefaultReliable)
This sends a network message with a message Id to the server. This message is sent on channel zero, which by default is the reliable channel.
void Update()
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.
bool UnregisterHandler< T >()
Unregisters a network message handler.
void Shutdown()
Shut down a client.

Public Properties

Name
NetworkConnection connection
The NetworkConnection object this client is using.

Public Attributes

Name
string serverIp
The IP address of the server that this client is connected to.
bool active
active is true while a client is connecting/connected (= while the network is active)
bool isConnected
This gives the current connection status of the client.
bool isLocalClient
NetworkClient can connect to local server in host mode too

Detailed Description

class Mirror::NetworkClient;

This is a network client class used by the networking system. It contains a NetworkConnection that is used to connect to a network server.

The [NetworkClient] handle connection state, messages handlers, and connection configuration. There can be many [NetworkClient] instances in a process at a time, but only one that is connected to a game server ([NetworkServer]) that uses spawned objects.

[NetworkClient] has an internal update function where it handles events from the transport layer. This includes asynchronous connect events, disconnect events and incoming data from a server.

The NetworkManager has a NetworkClient instance that it uses for games that it starts, but the NetworkClient may be used by itself.

Public Functions Documentation

function Connect

static inline void Connect(
    string address
)

Connect client to a NetworkServer instance.

Parameters:

  • address

function Connect

static inline void Connect(
    Uri uri
)

Connect client to a NetworkServer instance.

Parameters:

  • uri Address of the server to connect to

function ConnectHost

static inline void ConnectHost()

function ConnectLocalServer

static inline void ConnectLocalServer()

connect host mode

function DisconnectLocalServer

static inline void DisconnectLocalServer()

disconnect host mode. this is needed to call DisconnectMessage for the host client too.

function Disconnect

static inline void Disconnect()

Disconnect from server.

The disconnect message will be invoked.

function Send< T >

static inline void Send< T >(
    T message,
    int channelId =Channels.DefaultReliable
)

This sends a network message with a message Id to the server. This message is sent on channel zero, which by default is the reliable channel.

Parameters:

  • message
  • channelId

Template Parameters:

  • T The message type to unregister.

The message must be an instance of a class derived from MessageBase.

The message id passed to Send() is used to identify the handler function to invoke on the server when the message is received.

function Update

static inline void Update()

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 bool UnregisterHandler< T >()

Unregisters a network message handler.

Template Parameters:

  • T The message type to unregister.

function Shutdown

static inline void Shutdown()

Shut down a client.

This should be done when a client is no longer going to be used.

Public Property Documentation

property connection

static NetworkConnection connection;

The NetworkConnection object this client is using.

Public Attributes Documentation

variable serverIp

static string serverIp => connection.address;

The IP address of the server that this client is connected to.

This will be empty if the client has not connected yet.

variable active

static bool active => connectState == ConnectState.Connecting || connectState == ConnectState.Connected;

active is true while a client is connecting/connected (= while the network is active)

variable isConnected

static bool isConnected => connectState == ConnectState.Connected;

This gives the current connection status of the client.

variable isLocalClient

static bool isLocalClient => connection is ULocalConnectionToServer;

NetworkClient can connect to local server in host mode too


Updated on 24 January 2021 at 23:47:08 UTC