Shared Variables
The function createSharedVariable returns a SharedVariable object that should be used to install “listeners”. Those are callbacks that will be called whenever the variable value changes.
The callback is passed a reference to the variable object and can query the value property. It is also possible to poll on the lastUpdateTime property (initially 0) to detect when the value has been updated. The set method is used to update the
value of a variable. It will trigger any callback installed on the same variable object or on other variable objects created with the same name, including remote objects if networking is enabled.
Sample Code
Setting the Variable
On the player that controls the variable
var v=createSharedVariable( 'XY' ); v.set( 'new value' );
Reading the Variable
When the same player sets and read the variable:
function onUpdate( x ) {
//Do something based on x.value
}
var v=createSharedVariable( 'XY' ); v.addUpdateListener( onUpdate );
When a player sets the variable and another player reads its content:
function onUpdate( x ) {
//Do something based on x.value
}
var v=createSharedVariable( 'XY@controller' ); v.addUpdateListener( onUpdate );
Network API
Framing
The network protocol for the distribution of notifications is based on a stream of variable length commands sent over a TCP socket. Individual commands are separated by the \r\n (0x0D 0x0A)
happen inside the command. When string parameters are used, they are put between double quotes. Special characters should be escaped, in a manner similar to
newline (0x0A) with \n
carriage return (0x0D) with \r
double quote (0x22) with \" null byte (0x00) with \0
backslash itself with \\
Alternatively, the network API also supports setting shared variables via an HTTP GET request. This method does not allow persistent connections, but it is easier to access from a client scripting language supporting the XMLHttpRequest API, like JavaScript in web browsers.
Whenever a GET request is issued, the connection by the server is closed immediately after the response has been sent.
The HTTP GET interface supports only the 'UPDATE' and 'EVENT' commands.
86 | Inspired XPress - Programming Guide |