Noble Connect
Add relays and punchthrough to Mirror or UNet
NobleConnect.Mirror.NobleClient Class Reference

Adds relay, punchthrough, and port-forwarding support to the Mirror NetworkClient More...

Public Member Functions

 NobleClient (GeographicRegion region=GeographicRegion.AUTO, Action< string > onFatalError=null, int relayLifetime=60, int relayRefreshTime=30, float allocationResendTimeout=.1f, int maxAllocationResends=8, float requestTimeout=.2f)
 Initialize the client using NobleConnectSettings. The region used is determined by the Relay Server Address in the NobleConnectSettings. More...
 
 NobleClient ()
 
 NobleClient (string relayServerAddress, Action< string > onFatalError=null, int relayLifetime=60, int relayRefreshTime=30, float allocationResendTimeout=.1f, int maxAllocationResends=8, float requestTimeout=.2f)
 Initialize the client using NobleConnectSettings but connect to specific relay server address. This method is useful for selecting the region to connect to at run time when starting the client. More...
 
void PrepareToConnect ()
 Prepare to connect but don't actually connect yet More...
 
void Update ()
 If you are using the NetworkClient directly you must call this method every frame. More...
 
void Connect (string hostIP, ushort hostPort, bool isLANOnly=false)
 Connect to the provided host ip and port More...
 
void Connect (IPEndPoint hostEndPoint, bool isLANOnly=false)
 Connect to the provided HostEndPoint More...
 
void SetConnectPort (ushort port)
 
void Shutdown ()
 Shut down the client and clean everything up. More...
 
void Dispose (bool disposing)
 Clean up and free resources. Called automatically when garbage collected. More...
 
void Dispose ()
 
bool InvokeHandler< T > (T msg, int channelId)
 This function invokes the registered handler function for a message. More...
 
virtual void OnClientConnect (NetworkConnection conn)
 Called on the client upon succesfully connecting to a host More...
 
virtual void OnClientDisconnect (NetworkConnection conn)
 Called on the client upon disconnecting from a host More...
 
void Connect (string address)
 Connect client to a NetworkServer instance. More...
 
void Connect (Uri uri)
 Connect client to a NetworkServer instance. More...
 
void ConnectHost ()
 
void ConnectLocalServer ()
 connect host mode More...
 
void DisconnectLocalServer ()
 disconnect host mode. this is needed to call DisconnectMessage for the host client too. More...
 
void Disconnect ()
 Disconnect from server. More...
 
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. More...
 
void RegisterHandler< T > (Action< NetworkConnection, T > handler, bool requireAuthentication=true)
 Register a handler for a particular message type. More...
 
void RegisterHandler< T > (Action< T > handler, bool requireAuthentication=true)
 Register a handler for a particular message type. More...
 
void ReplaceHandler< T > (Action< NetworkConnection, T > handler, bool requireAuthentication=true)
 Replaces a handler for a particular message type. More...
 
void ReplaceHandler< T > (Action< T > handler, bool requireAuthentication=true)
 Replaces a handler for a particular message type. More...
 
bool UnregisterHandler< T > ()
 Unregisters a network message handler. More...
 

Public Attributes

bool isConnecting = false
 A convenient way to check if a connection is in progress More...
 
bool ForceRelayOnly
 Store force relay so that we can pass it on to the iceController More...
 
NetworkConnection connection => NetworkClient.connection
 The rest of this is just a wrapper for Mirror's NetworkClient. More...
 
bool active => NetworkClient.active
 active is true while a client is connecting/connected (= while the network is active) More...
 
bool isConnected => NetworkClient.isConnected
 This gives the current connection status of the client. More...
 
bool isLocalClient => NetworkClient.isLocalClient
 NetworkClient can connect to local server in host mode too More...
 

Properties

ConnectionType latestConnectionType [get, set]
 You can check this in OnClientConnect(), it will either be Direct, Punchthrough, or Relay. More...
 

Detailed Description

Adds relay, punchthrough, and port-forwarding support to the Mirror NetworkClient

Use the Connect method to connect to a host.

Constructor & Destructor Documentation

◆ NobleClient() [1/3]

NobleConnect.Mirror.NobleClient.NobleClient ( GeographicRegion  region = GeographicRegion.AUTO,
Action< string >  onFatalError = null,
int  relayLifetime = 60,
int  relayRefreshTime = 30,
float  allocationResendTimeout = .1f,
int  maxAllocationResends = 8,
float  requestTimeout = .2f 
)

Initialize the client using NobleConnectSettings. The region used is determined by the Relay Server Address in the NobleConnectSettings.

The default address is connect.noblewhale.com, which will automatically select the closest server based on geographic region.

If you would like to connect to a specific region you can use one of the following urls:

    us-east.connect.noblewhale.com - Eastern United States
    us-west.connect.noblewhale.com - Western United States
    eu.connect.noblewhale.com - Europe
    ap.connect.noblewhale.com - Asia-Pacific
    sa.connect.noblewhale.com - South Africa
    hk.connect.noblewhale.com - Hong Kong

Note that region selection will ensure each player connects to the closest relay server, but it does not prevent players from connecting across regions. If you want to prevent joining across regions you will need to implement that separately (by filtering out unwanted regions during matchmaking for example).

Parameters
topoThe HostTopology to use for the NetworkClient. Must be the same on host and client.
onFatalErrorA method to call if something goes horribly wrong.
allocationResendTimeoutInitial timeout before resending refresh messages. This is doubled for each failed resend.
maxAllocationResendsMax number of times to try and resend refresh messages before giving up and shutting down the relay connection. If refresh messages fail for 30 seconds the relay connection will be closed remotely regardless of these settings.

◆ NobleClient() [2/3]

NobleConnect.Mirror.NobleClient.NobleClient ( )

◆ NobleClient() [3/3]

NobleConnect.Mirror.NobleClient.NobleClient ( string  relayServerAddress,
Action< string >  onFatalError = null,
int  relayLifetime = 60,
int  relayRefreshTime = 30,
float  allocationResendTimeout = .1f,
int  maxAllocationResends = 8,
float  requestTimeout = .2f 
)

Initialize the client using NobleConnectSettings but connect to specific relay server address. This method is useful for selecting the region to connect to at run time when starting the client.

Parameters
relayServerAddressThe url or ip of the relay server to connect to
topoThe HostTopology to use for the NetworkClient. Must be the same on host and client.
onFatalErrorA method to call if something goes horribly wrong.
allocationResendTimeoutInitial timeout before resending refresh messages. This is doubled for each failed resend.
maxAllocationResendsMax number of times to try and resend refresh messages before giving up and shutting down the relay connection. If refresh messages fail for 30 seconds the relay connection will be closed remotely regardless of these settings.

Member Function Documentation

◆ Connect() [1/4]

void NobleConnect.Mirror.NobleClient.Connect ( IPEndPoint  hostEndPoint,
bool  isLANOnly = false 
)

Connect to the provided HostEndPoint

Note that the host address used here should be the one provided to the host by the relay server, not the actual ip of the host's computer. You can get this address on the host from Server.HostEndPoint.

Parameters
hostEndPointThe HostEndPoint of the server to connect to
hostPortThe port of the server's HostEndPoint
topoThe HostTopology to use for the NetworkServer.

◆ Connect() [2/4]

void NobleConnect.Mirror.NobleClient.Connect ( string  address)

Connect client to a NetworkServer instance.

Parameters
address

◆ Connect() [3/4]

void NobleConnect.Mirror.NobleClient.Connect ( string  hostIP,
ushort  hostPort,
bool  isLANOnly = false 
)

Connect to the provided host ip and port

Note that the host address used here should be the one provided to the host by the relay server, not the actual ip of the host's computer. You can get this address on the host from Server.HostEndPoint.

Parameters
hostIPThe IP of the server's HostEndPoint
hostPortThe port of the server's HostEndPoint
topoThe HostTopology to use for the NetworkServer.

◆ Connect() [4/4]

void NobleConnect.Mirror.NobleClient.Connect ( Uri  uri)

Connect client to a NetworkServer instance.

Parameters
uriAddress of the server to connect to

◆ ConnectHost()

void NobleConnect.Mirror.NobleClient.ConnectHost ( )

◆ ConnectLocalServer()

void NobleConnect.Mirror.NobleClient.ConnectLocalServer ( )

connect host mode

◆ Disconnect()

void NobleConnect.Mirror.NobleClient.Disconnect ( )

Disconnect from server.

The disconnect message will be invoked.

◆ DisconnectLocalServer()

void NobleConnect.Mirror.NobleClient.DisconnectLocalServer ( )

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

◆ Dispose() [1/2]

void NobleConnect.Mirror.NobleClient.Dispose ( )

◆ Dispose() [2/2]

void NobleConnect.Mirror.NobleClient.Dispose ( bool  disposing)

Clean up and free resources. Called automatically when garbage collected.

You shouldn't need to call this directly. It will be called automatically when an unused NobleClient is garbage collected or when shutting down the application.

Parameters
disposing

◆ InvokeHandler< T >()

bool NobleConnect.Mirror.NobleClient.InvokeHandler< T > ( msg,
int  channelId 
)

This function invokes the registered handler function for a message.

Network connections used by the NetworkClient and NetworkServer use this function for handling network messages.

Template Parameters
TThe message type to unregister.
Parameters
msgThe message object to process.
Returns
Returns true if the handler was successfully invoked
Type Constraints
T :struct 
T :NetworkMessage 

◆ OnClientConnect()

virtual void NobleConnect.Mirror.NobleClient.OnClientConnect ( NetworkConnection  conn)
virtual

Called on the client upon succesfully connecting to a host

We clean some ice stuff up here.

Parameters
message

◆ OnClientDisconnect()

virtual void NobleConnect.Mirror.NobleClient.OnClientDisconnect ( NetworkConnection  conn)
virtual

Called on the client upon disconnecting from a host

Some memory and ports are freed here.

Parameters
message

◆ PrepareToConnect()

void NobleConnect.Mirror.NobleClient.PrepareToConnect ( )

Prepare to connect but don't actually connect yet

This is used when initializing a client early before connecting. Getting this out of the way earlier can make the actual connection seem quicker.

◆ RegisterHandler< T >() [1/2]

void NobleConnect.Mirror.NobleClient.RegisterHandler< T > ( Action< NetworkConnection, T >  handler,
bool  requireAuthentication = true 
)

Register a handler for a particular message type.

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

Template Parameters
TMessage type
Parameters
handlerFunction handler which will be invoked when this message type is received.
requireAuthenticationTrue if the message requires an authenticated connection
Type Constraints
T :struct 
T :NetworkMessage 

◆ RegisterHandler< T >() [2/2]

void NobleConnect.Mirror.NobleClient.RegisterHandler< T > ( Action< T >  handler,
bool  requireAuthentication = true 
)

Register a handler for a particular message type.

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

Template Parameters
TMessage type
Parameters
handlerFunction handler which will be invoked when this message type is received.
requireAuthenticationTrue if the message requires an authenticated connection
Type Constraints
T :struct 
T :NetworkMessage 

◆ ReplaceHandler< T >() [1/2]

void NobleConnect.Mirror.NobleClient.ReplaceHandler< T > ( Action< NetworkConnection, T >  handler,
bool  requireAuthentication = true 
)

Replaces a handler for a particular message type.

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

Template Parameters
TMessage type
Parameters
handlerFunction handler which will be invoked when this message type is received.
requireAuthenticationTrue if the message requires an authenticated connection
Type Constraints
T :struct 
T :NetworkMessage 

◆ ReplaceHandler< T >() [2/2]

void NobleConnect.Mirror.NobleClient.ReplaceHandler< T > ( Action< T >  handler,
bool  requireAuthentication = true 
)

Replaces a handler for a particular message type.

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

Template Parameters
TMessage type
Parameters
handlerFunction handler which will be invoked when this message type is received.
requireAuthenticationTrue if the message requires an authenticated connection
Type Constraints
T :struct 
T :NetworkMessage 

◆ Send< T >()

void NobleConnect.Mirror.NobleClient.Send< 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.

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.

Template Parameters
TThe message type to unregister.
Parameters
message
channelId
Type Constraints
T :struct 
T :NetworkMessage 

◆ SetConnectPort()

void NobleConnect.Mirror.NobleClient.SetConnectPort ( ushort  port)

◆ Shutdown()

void NobleConnect.Mirror.NobleClient.Shutdown ( )

Shut down the client and clean everything up.

You can call this method if you are totally done with a client and don't plan on using it to connect again.

◆ UnregisterHandler< T >()

bool NobleConnect.Mirror.NobleClient.UnregisterHandler< T > ( )

Unregisters a network message handler.

Template Parameters
TThe message type to unregister.
Type Constraints
T :struct 
T :NetworkMessage 

◆ Update()

void NobleConnect.Mirror.NobleClient.Update ( )

If you are using the NetworkClient directly you must call this method every frame.

The NobleNetworkManager and NobleNetworkLobbyManager handle this for you but you if you are using the NobleClient directly you must make sure to call this method every frame.

Member Data Documentation

◆ active

bool NobleConnect.Mirror.NobleClient.active => NetworkClient.active

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

◆ connection

NetworkConnection NobleConnect.Mirror.NobleClient.connection => NetworkClient.connection

The rest of this is just a wrapper for Mirror's NetworkClient.

The NetworkConnection object this client is using.

◆ ForceRelayOnly

bool NobleConnect.Mirror.NobleClient.ForceRelayOnly

Store force relay so that we can pass it on to the iceController

◆ isConnected

bool NobleConnect.Mirror.NobleClient.isConnected => NetworkClient.isConnected

This gives the current connection status of the client.

◆ isConnecting

bool NobleConnect.Mirror.NobleClient.isConnecting = false

A convenient way to check if a connection is in progress

◆ isLocalClient

bool NobleConnect.Mirror.NobleClient.isLocalClient => NetworkClient.isLocalClient

NetworkClient can connect to local server in host mode too

Property Documentation

◆ latestConnectionType

ConnectionType NobleConnect.Mirror.NobleClient.latestConnectionType
getset

You can check this in OnClientConnect(), it will either be Direct, Punchthrough, or Relay.