Sites

Menu
These are the docs for 14.0, an old version of SpatialOS. The docs for this version are frozen: we do not correct, update or republish them. 14.1 is the newest →

Connecting to SpatialOS

All code examples in this section assume using Improbable.Worker; and using Example; (the generated code namespace).

Before it can interact with the simulated world, a worker must connect to SpatialOS.

There are two ways of doing this: using the Connection and using the Locator.

Connecting a managed worker

To connect a managed worker, instantiate a Improbable.Worker.Connection object directly. Note that Connection objects are not thread-safe.

This should be used to connect a managed worker, or a worker connecting for debugging purposes via spatial cloud connect external.

The connection is made using the receptionist service of the target deployment; the IP and port for that deployment are passed as arguments to the worker. To have these values filled in automatically, use the IMPROBABLE_RECEPTIONIST_HOST and IMPROBABLE_RECEPTIONIST_PORT placeholders in your worker’s launch configuration.

Connecting an external/client worker

Use the Improbable.Worker.Locator object to enumerate cloud deployments and connect to a chosen deployment with authentication.

This is typically used to connect clients to a cloud deployment.

Values for UseExternalIP

You need to use the correct value for the UseExternalIp field in NetworkParameters. The table below summarizes the connection possibilities:

Using Connection directly Using Locator
UseExternalIp == true Local client connecting via spatial cloud connect external proxy External client connecting to cloud deployment
UseExternalIp == false Managed cloud worker, or local client connecting to local deployment Local client connecting to a local deployment as if it were a cloud deployment.

Connecting to a local Locator

When running the local API service, local clients can connect to local deployments using the local Locator. This allows clients to use largely the same code when connecting to either local or cloud deployments.

When connecting to the local Locator, you need to change your connection to connect to “localhost” and your local API port (rather than “locator.improbable.io” and 443). By default the local API port is 9876 but you can also choose it when starting the local API service). You also need to enable the LocatorParameter UseInsecureConnection, for example:

var locatorParameters = new LocatorParameters
{
    UseInsecureConnection = true
};
var locator = new Locator("localhost", localAPIPort, locatorParameters);

Example connection setup

The example below illustrates a very basic connection setup, where the function takes three arguments specifying the worker’s own ID as well as the receptionist’s IP and port. It uses TCP and connects using the internal IP address.

private static Connection GetConnection(string workerId, string hostname, ushort port)
{
  var parameters = new ConnectionParameters
  {
    WorkerType = "MyCsharpWorker",
    Network =
    {
      ConnectionType = NetworkConnectionType.Tcp,
      UseExternalIp = false
    }
  };
  return Connection.ConnectAsync(hostname, port, workerId, parameters).Get();
}

Worker attributes

A worker instance is assigned some static attributes when it connects to the SpatialOS Runtime:

  • a worker ID which is a unique identifier string for this particular worker instance.
  • a worker attribute that is used to determine which layer the worker instance is part of, and therefore which components it can simulate, either automatically or by the developer through ACLs.

Get the values of these configuration settings using Connection.GetWorkerId() and Connection.GetWorkerAttributes().

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums