xxxxxxxxxx
// https://p5js.org/reference/
const SERVER = 'SERVER';
const USER = 'USER';
class Server {
constructor (serverID, maxUserCount = 10) {
if (serverID == null) {
this.serverID = generateID();
} else {
this.serverID = serverID;
}
this.type = SERVER;
this.maxUserCount = maxUserCount;
this.messages = [];
this.users = [];
}
getID () {
return this.serverID;
}
}
class User {
constructor(name, uuid) {
this.type = USER;
this.name = name;
if (uuid == null) {
this.UUID = generateID();
} else {
this.UUID = uuid;
}
this.currentServerID = '';
}
getID() {
return this.UUID;
}
changeName (newName) {
this.name = newName;
}
sentMessage(contents) {
let message = new Message(contents, currentServerID, UUID);
// socket.emit('sendMessageClient', message);
}
}
class Message {
constructor (contents, serverID, senderID, timeSent, isPrivate, receiverID) {
this.contents = contents;
this.serverID = serverID;
this.senderID = senderID;
if (timeSent == null) {
this.timeSent = year().toString() + '-' + month().toString() + '-' + day().toString() + '-' + hour().toString() + '-' + minute().toString() + '-' + second().toString() + '-' + millis().toString(); // formatted date
} else {
this.timeSent = timeSent;
}
this.isPrivate = isPrivate;
this.receiverID = receiverID;
}
}
const client = new User();
const server = new Server();
var socket = io.connect($OP.getEchoServerURL(2595874));
var IS_SERVER = false;
function setup() {
createCanvas(windowWidth, windowHeight);
// socket example: https://openprocessing.org/sketch/422520
// socket.emit('serverCheck');
socket.on('sendMessageClient', newMessageServer);
// console.log(generateID());
// console.log(client.getID());
// console.log(server.getID());
}
function draw() {
background(100);
noStroke();
if (IS_SERVER) {
circle(width/2, height/2, 25);
for (let i = 0; i < server.messages.length; i++) {
console.log(server.messages[i]);
}
} else {
rect(width/2, height/2, 25, 25);
}
if (keyIsDown(90) && keyIsDown(83)) { // if z and s are pressed at the same time, server mode
console.log('server mode ' + server.getID());
IS_SERVER = true;
}
if (keyIsDown(90) && keyIsDown(67)) { // if z and c are pressed at the same time, client mode
console.log('client mode ' + client.getID());
IS_SERVER = false;
}
}
function newMessageServer(message) {
if (IS_SERVER) {}
}
function keyPressed() {
console.log(key + ' - ' + keyCode);
// if (key === 'c') {
// // Code to run.
// }
// if (keyCode === ENTER) {
// // Code to run.
// }
}
function getRandomInt(max) { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
return Math.floor(Math.random() * max);
}
function generateID(length = 7) {
let ID = '';
for (let i = 0; i < length; i++) {
ID += getRandomInt(9).toString();
}
return ID;
}
var socket = io.connect($OP.getEchoServerURL(2595874));
Learn more See an example