Connecting to SpatialOS
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 |
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 is assigned some attributes when it connects to SpatialOS:
- A worker ID which is a unique identifier string for this particular worker.
- A list of the worker’s attributes. (Used to determine which worker can be given authority over a component, these are either created automatically or user-created using ACLs.)
Get the values of these configuration settings using
Connection.GetWorkerId()
and Connection.GetWorkerAttributes()
.