Using a Web-interface to a breve Simulation
<< previous | Chapter 12. Networking | next >>

## Using a Web-interface to a breve Simulation

The NetworkServer object can accept connections from regular web browsers. After the NetworkServer starts running, you can get the address that clients should connect to by using the method get-url. The URL will be the hostname (or IP address) of the server machine, followed by ":`portNumber`".

When a web-client connects to the server, it may do one of two things: execute a command in the simulation's controller object, or request an HTML page.

### Executing Controller Commands Via Web Requests

To execute a method in the controller instance, simply append the method name to the end of the URL. If you wish to pass in either int or float arguments, they can be added afterwords, delimited by underscores. Other types may currently not be passed via the web interface. Here are some examples:

    # call the Controller method "turn-agent-blue"
    http://myserver:33333/turn-agent-blue

    # call the Controller method "set-agent-color" with three float type arguments.
    http://myserver:33333/set-agent-color_.2_.4_.6

The controller methods called may optionally return a string which will determine what information is sent back to the web browser. If the string ends with ".html", breve will try to load data from an HTML file on the local disk and send it back to the web browser (sending HTML files is described in more detail in the next section). Otherwise, the string itself is returned to the web-broswer.

### Serving HTML Pages Via Web Requests

If an HTTP request to a breve network server ends with ".html", breve will try to provide the web browser with an HTML file from the local disk. This feature is not typically used to serve informational web files as a normal web server might—instead, it should be used to serve files containing "links" that will execute controller commands in the simulation, as described above (the section called “Executing Controller Commands Via Web Requests”).

Additionally, you can set an "index" page that will be served to the web browser when it sends a request to your server without an actual page or method request, for example, http://myserver:33333. This index page can be set using the method set-index-page.

<< previous | Up | next >>
Sending and Receiving breve Objects | Table of Contents | Chapter 13. Plugins