Home | Reference | Development Notes
// 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();
}Sets up client to connect to server and send messages to host.
setupClient()None
None
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);Sends JavaScript object message of specified data type from client to host.
sendData(datatype, data)datatype String: data type of message
data Object: a JavaScript object containing user-defined values
None
function draw() {
background(0);
if(isClientConnected(display=true)) {
// Client draw here. ---->
// <----
}
}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.
isClientConnected(display)display Boolean: displays connectivity support instructions if true (default is false)
Boolean: true if client is connected, false otherwise
User-defined callbacks for handling data received from hosts. These must be present in your index.js sketch.
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.
*/
}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:
.idString: unique ID of host.typeString: data type of message
onReceiveData (data)data Object: contains all data sent from client
None
// 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();
}Sets up host to connect to server and receive messages from clients.
setupHost()None
None
function mousePressed() {
sendData('timestamp', { timestamp: millis() });
}let myData = {
val1: 0,
val2: 128,
val3: true
}
sendData('myDataType', myData);Sends JavaScript object message of specified data type from host to all connected clients.
sendData(datatype, data)datatype String: data type of message
data Object: a JavaScript object containing user-defined values
None
function draw () {
background(15);
if(isHostConnected(display=true)) {
// Host/Game draw here. --->
// <----
// Display server address
displayAddress();
}
}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.
isHostConnected(display)display Boolean: displays connectivity status if true (default is false)
Boolean: true if host is connected, false otherwise
function draw () {
background(15);
if(isHostConnected(display=true)) {
// Host/Game draw here. --->
// <----
// Display server address
displayAddress();
}
}Displays the server address in the lower left of the canvas.
displayAddress()None
None
User-defined callbacks for handling client connections and disconnections and data received from clients. These must be present in your host.js sketch.
function onClientConnect (data) {
// Client connect logic here. --->
print(data.id + ' has connected.');
// <----
}Callback for when new client connects to server. Must be defined in host.js sketch. data parameter provides:
.idString: unique ID of client
onClientConnect (data)data Object: contains client connection data
None
function onClientDisconnect (data) {
// Client connect logic here. --->
print(data.id + ' has disconnected.');
// <----
}Callback for when client disconnects from server. Must be defined in host.js sketch. data parameter provides:
.idString: unique ID of client
onClientDisconnect (data)data Object: contains client connection data
None
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.
*/
}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:
.idString: unique ID of client.typeString: data type of message
onReceiveData (data)data Object: contains all data sent from client
None