You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zomburger/docs/REFERENCE.md

7.5 KiB

Home | Reference | Development Notes

Reference

Client

Host


Client


setupClient()

[Back to top]

Example
// Network settings
const serverIp      = '127.0.0.1';
const serverPort    = '3000';
const local         = true;   // true if running locally, false
                              // if running on remote server

function setup() {
  createCanvas(windowWidth, windowHeight);

  setupClient();
}
Description

Sets up client to connect to server and send messages to host.

Syntax
setupClient()
Parameters

None

Returns

None


sendData() - Client

[Back to top]

Example
sendData('playerColor', {
  r: red(playerColor)/255,
  g: green(playerColor)/255,
  b: blue(playerColor)/255
});
let myData = {
    val1: 0,
    val2: 128,
    val3: true
}

sendData('myDataType', myData);
Description

Sends JavaScript object message of specified data type from client to host.

Syntax
sendData(datatype, data)
Parameters

datatype String: data type of message data Object: a JavaScript object containing user-defined values

Returns

None


isClientConnected()

[Back to top]

Example
function draw() {
  background(0);

  if(isClientConnected(display=true)) {
    // Client draw here. ---->


    // <----
  }
}
Description

Checks to see if the client is successfully connected to the server and returns Boolean result. If display=true, connectivity support instructions are displayed on the screen.

Syntax
isClientConnected(display)
Parameters

display Boolean: displays connectivity support instructions if true (default is false)

Returns

Boolean: true if client is connected, false otherwise


Client Callbacks

User-defined callbacks for handling data received from hosts. These must be present in your index.js sketch.


onReceiveData() - Client

[Back to top]

Example
function onReceiveData (data) {
  // Input data processing here. --->
  console.log(data);
  
  // <---

  /* Example:
     if (data.type === 'myDataType') {
       processMyData(data);
     }

     Use `data.type` to get the message type sent by host.
  */

}
Description

Callback for when data is received from a host. Must be defined in index.js sketch. data parameter provides all data sent from host and always includes:

  • .id String: unique ID of host
  • .type String: data type of message
Syntax
onReceiveData (data)
Parameters

data Object: contains all data sent from client

Returns

None


Host


setupHost()

[Back to top]

Example
// Network settings
const serverIp      = '127.0.0.1';
const serverPort    = '3000';
const local         = true;   // true if running locally, false
                              // if running on remote server

function setup() {
  createCanvas(windowWidth, windowHeight);

  setupHost();
}
Description

Sets up host to connect to server and receive messages from clients.

Syntax
setupHost()
Parameters

None

Returns

None


sendData() - Host

[Back to top]

Example
function mousePressed() {
  sendData('timestamp', { timestamp: millis() });
}
let myData = {
    val1: 0,
    val2: 128,
    val3: true
}

sendData('myDataType', myData);
Description

Sends JavaScript object message of specified data type from host to all connected clients.

Syntax
sendData(datatype, data)
Parameters

datatype String: data type of message data Object: a JavaScript object containing user-defined values

Returns

None


isHostConnected()

[Back to top]

Example
function draw () {
  background(15);

  if(isHostConnected(display=true)) {
    // Host/Game draw here. --->


    // <----

    // Display server address
    displayAddress();
  }
}
Description

Checks to see if the host is successfully connected to the server and returns Boolean result. If display=true, connectivity status is displayed on the screen.

Syntax
isHostConnected(display)
Parameters

display Boolean: displays connectivity status if true (default is false)

Returns

Boolean: true if host is connected, false otherwise


displayAddress()

[Back to top]

Example
function draw () {
  background(15);

  if(isHostConnected(display=true)) {
    // Host/Game draw here. --->


    // <----

    // Display server address
    displayAddress();
  }
}
Description

Displays the server address in the lower left of the canvas.

Syntax
displayAddress()
Parameters

None

Returns

None


Host Callbacks

User-defined callbacks for handling client connections and disconnections and data received from clients. These must be present in your host.js sketch.


onClientConnect()

[Back to top]

Example
function onClientConnect (data) {
  // Client connect logic here. --->
  print(data.id + ' has connected.');

  // <----
}
Description

Callback for when new client connects to server. Must be defined in host.js sketch. data parameter provides:

  • .id String: unique ID of client
Syntax
onClientConnect (data)
Parameters

data Object: contains client connection data

Returns

None


onClientDisconnect()

[Back to top]

Example
function onClientDisconnect (data) {
  // Client connect logic here. --->
  print(data.id + ' has disconnected.');

  // <----
}
Description

Callback for when client disconnects from server. Must be defined in host.js sketch. data parameter provides:

  • .id String: unique ID of client
Syntax
onClientDisconnect (data)
Parameters

data Object: contains client connection data

Returns

None


onReceiveData() - Host

[Back to top]

Example
function onReceiveData (data) {
  // Input data processing here. --->
  console.log(data);
  
  // <---

  /* Example:
     if (data.type === 'myDataType') {
       processMyData(data);
     }

     Use `data.type` to get the message type sent by client.
  */

}
Description

Callback for when data is received from a client. Must be defined in host.js sketch. data parameter provides all data sent from client and always includes:

  • .id String: unique ID of client
  • .type String: data type of message
Syntax
onReceiveData (data)
Parameters

data Object: contains all data sent from client

Returns

None