We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone I have a new problem. I made this program that connects the server and client to each other. But I must make sure that every time a new client connects to be asked: username and a color of your choice, and that these data are saved for each user. how can I do?
import processing.net.*;
// Declare a server
Server server;
// Used to indicate a new message has arrived
float newMessageColor = 255;
PFont f;
String incomingMessage = "";
void setup() {
size(400,200);
// Create the Server on port 5204
server = new Server(this, 5204);
f = createFont("Arial",16,true);
}
void draw() {
background(newMessageColor);
// newMessageColor fades to white over time
newMessageColor = constrain(newMessageColor + 0.3,0,255);
textFont(f);
textAlign(CENTER);
fill(255);
// The most recent incoming message is displayed in the window.
text(incomingMessage,width/2,height/2);
// If a client is available, we will find out
// If there is no client, it will be"null"
Client client = server.available();
// We should only proceed if the client is not null
if (client!= null) {
// Receive the message
// The message is read using readString().
incomingMessage = client.readString();
// The trim() function is used to remove the extra line break that comes in with the message.
incomingMessage = incomingMessage.trim();
// Print to Processing message window
println( "Client says:" + incomingMessage);
// Write message back out (note this goes to ALL clients)
server.write( "How does " + incomingMessage + " make you feel?\n" ); // A reply is sent using write().
// Reset newMessageColor to black
newMessageColor = 0;
}
}
// The serverEvent function is called whenever a new client connects.
void serverEvent(Server server, Client client) {
incomingMessage = "A new client has connected: " + client.ip();
println(incomingMessage);
// Reset newMessageColor to black
newMessageColor = 0;
}
Client
import processing.net.*;
// Declare a client
Client client;
// Used to indicate a new message
float newMessageColor = 0;
// A String to hold whatever the server says
String messageFromServer = "";
// A String to hold what the user types
String typing ="";
PFont f;
void setup() {
size(400,200);
// Create the Client, connect to server at 127.0.0.1 (localhost), port 5204
client = new Client(this,"127.0.0.1", 5204);
f = createFont("Arial",16,true);
}
void draw() {
background(255);
// Display message from server
fill(newMessageColor);
textAlign(CENTER);
text(messageFromServer,width/2,140);
// Fade message from server to white
newMessageColor = constrain(newMessageColor+1,0,255);
// Display Instructions
fill(0);
text(" Nickname: ",100,50);
// Display text typed by user
fill(0);
text(typing,160,50);
// If there is information available to read
// (we know there is a message from the Server when there are greater than zero bytes available.)
if (client.available() > 0) {
// Read it as a String
messageFromServer = client.readString();
// Set brightness to 0
newMessageColor = 0;
}
}
// Simple user keyboard input
void keyPressed() {
// If the return key is pressed, save the String and clear it
if (key == '\n') {
// When the user hits enter, write the sentence out to the Server
println("ok");
} else {
typing = typing + key;
}
}
Comments
Well, you're gonna need a class (or lotsa arrays) to store info related to each new client! :-t
Also, save & load that database from disk!
can you give me an input to start?
up :(