Skip to content

Commit

Permalink
feat: new way to connect using uri (#1279)
Browse files Browse the repository at this point in the history
* feat: new way to connect using uri

* set the host name when starting by url
  • Loading branch information
paulpach authored and miwarnec committed Dec 5, 2019
1 parent 05dd3b6 commit 7c3622c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public static void Connect(string address)
connection.SetHandlers(handlers);
}

/// <summary>
/// Connect client to a NetworkServer instance.
/// </summary>
/// <param name="uri">Address of the server to connect to</param>
public static void Connect(Uri uri)
{
if (LogFilter.Debug) Debug.Log("Client Connect: " + uri);

RegisterSystemHandlers(false);
Transport.activeTransport.enabled = true;
InitializeTransportHandlers();

connectState = ConnectState.Connecting;
Transport.activeTransport.ClientConnect(uri);

// setup all the handlers
connection = new NetworkConnectionToServer();
connection.SetHandlers(handlers);
}

internal static void SetupLocalConnection()
{
if (LogFilter.Debug) Debug.Log("Client Connect Local Server");
Expand Down
31 changes: 31 additions & 0 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,37 @@ public void StartClient()
OnStartClient();
}

/// <summary>
/// This starts a network client. It uses the networkAddress and networkPort properties as the address to connect to.
/// <para>This makes the newly created client connect to the server immediately.</para>
/// </summary>
/// <param name="uri">location of the server to connect to</param>
public void StartClient(Uri uri)
{
InitializeSingleton();

if (authenticator != null)
{
authenticator.OnStartClient();
authenticator.OnClientAuthenticated.AddListener(OnClientAuthenticated);
}

if (runInBackground)
Application.runInBackground = true;

isNetworkActive = true;

RegisterClientMessages();

if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + uri);
this.networkAddress = uri.Host;

NetworkClient.Connect(uri);

OnStartClient();
}


/// <summary>
/// This starts a network "host" - a server and client in the same application.
/// <para>The client returned from StartHost() is a special "local" client that communicates to the in-process server using a message queue instead of the real network. But in almost all other cases, it can be treated as a normal client.</para>
Expand Down
12 changes: 12 additions & 0 deletions Assets/Mirror/Runtime/Transport/Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public abstract class Transport : MonoBehaviour
/// <param name="address">The IP address or FQDN of the server we are trying to connect to</param>
public abstract void ClientConnect(string address);


/// <summary>
/// Establish a connecion to a server
/// </summary>
/// <param name="uri">The address of the server we are trying to connect to</param>
public virtual void ClientConnect(Uri uri)
{
// By default, to keep backwards compatibility, just connect to the host
// in the uri
ClientConnect(uri.Host);
}

/// <summary>
/// Send data to the server
/// </summary>
Expand Down

0 comments on commit 7c3622c

Please sign in to comment.