Create a directory under
workers/in your SpatialOS project (for example
Create a new worker configuration file in this directory named
<your_worker_type>with a name of your choice, such as
js_worker- and add the following contents:
Note: The provided file uses default build scripts. If you need to customize it further, you can turn off automatic build script generation.
Create an HTML page in the root of your worker directory named
<script>tag in the
Note: Other resources in addition to
index.htmlinside your worker folder need to be accessed with the
resources/endpoint. This is detailed below under Accessing the worker. An absolute path is recommended because a relative path can point to different places depending on the end point used to access
Create a new file in the
index.js(which will act as your entry point). Add a small amount of code which connects to a deployment. Insert the following code to
<your_worker_type>as appropriate in the connection parameters:
This starter code makes use of the local locator, which currently ignores
locatorParameters.projectNameand the deployment name in
locator.connect(...). After these steps are complete, your directory tree should now look like the following:
+-- workers/<your_directory_name> +-- src | \-- index.js |-- index.html \-- spatialos.<your_worker_type>.worker.json
With the above files in place, run
spatial worker build <your_worker_type>. This will create a number of files including a npm
webpack.config.js. These will then be used to create the
build/demo.jsfile as directed by
webpack.config.js. More information can be found under Project structure.
Launch a local deployment by running
spatial local launch. You can run your worker by visiting
localhost:21000/worker/<worker_folder>in the browser. For example, if your worker is inside the
workers/js_workerfolder in your SpatialOS project, you can run it by visiting
localhost:21000/worker/js_worker. More information can be found under Accessing the worker.
When using generated build scripts, the following project structure will be generated:
npm is configured to use webpack,
which will package all your source code into a single JS file called
demo.js inside the folder
spatial worker build will invoke
npm run build inside the worker directory. This will
download and install
webpack, then will invoke
src into the final
build/demo.js file, which is loaded by the web page.
Accessing the worker
When a local deployment is launched, a web server is started at
localhost:21000. This usually hosts
the inspector at
endpoints available, which are:
index.htmlinside your worker folder. A
/can optionally be added to the end of this URL, which will also redirect to
localhost:21000/worker/<your_directory_name>/resources/<path>- This will redirect to any path
<path>inside your worker folder. For example:
build/demo.jsfrom your worker folder.
Was this page helpful?
Thanks for letting us know!
Thanks for your feedback
Need more help? Ask on the forums