We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a problem with this program. I have to make a connection between server and client. The client, once connected, enter nickname and choose a color, then press the button and I have to save it in a variable (in need) the nickname inserted, but does not work (the name is written on the screen and not saved). How do I?
import processing.net.*;
// Declare a server
Server server;
// Used to indicate a new message has arrived
float newMessageColor = 255;
PFont f;
String incomingMessage = "";
float x = random(0,400);
float y = random(0,200);
String nome;
color c=color(0,0,0);
Utente [] utente;
void setup() {
size(400,200);
// Create the Server on port 5204
server = new Server(this, 5204);
f = createFont("Arial",16,true);
utente = new Utente [10];
for(int i = 9; i>=0; i--){
utente[i] = new Utente(nome,c);
println("Colore client 0"+utente[i].colore);
println("Nome client 0"+utente[i].nome);
}
}
void draw() {
background(newMessageColor);
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.*;
// Dichiaro un client
Client client;
//
// Stati
final int statoMenu = 0;
final int statoColore = 1;
final int intermedio = 2;
final int play = 3;
int stato;
//----------------------------------
String nome = " ";
PFont font;
color c = color(0, 0, 0);
void setup() {
size(700, 500);
client = new Client(this,"127.0.0.1", 5204);
font = createFont("Helvetica", 18);
}
void draw() {
background(255);
textFont(font,18);
fill(255,0,0);
// this adds a blinking cursor after your text, at the expense of redrawing everything every frame
if (stato == statoMenu || stato == statoColore || stato == intermedio){
text("Nickname:",40,40);
text(nome +(frameCount/10 % 2 == 0 ? "_" : ""), 125, 40);
}
if (stato == statoColore || stato == intermedio){
colore();
}
if (stato == intermedio){
rect(100, 100, 50, 20);
}
}
void keyPressed() {
switch(stato){
case statoMenu:
keyPressedMenu();
break;
}
}
void keyPressedMenu(){
if (key != CODED) {
switch(key) {
case BACKSPACE:
nome = nome.substring(0,max(0,nome.length()-1));
stato = statoMenu;
break;
case TAB:
nome += " ";
break;
case ENTER:
stato=statoColore;
break;
/*case RETURN:
// comment out the following two lines to disable line-breaks
typedText += "\n";*/
case ESC:
exit();
break;
default:
nome += key;
}
}
}
void colore(){
//red
fill(255, 0, 0);
rect(50, 50, 20, 20);
//pink
fill(255, 0, 255);
rect(90, 50, 20, 20);
//purple
fill(130, 0, 255);
rect(140, 50, 20, 20);
}
void mousePressed(){
if (stato == statoColore){
if(mouseX > 50 && mouseX < 70 && mouseY > 50 && mouseY < 70){
c = color(255, 0, 0);
stato = intermedio;
}
if(mouseX > 90 && mouseX < 110 && mouseY > 50 && mouseY <70){
c = color(255, 0, 255);
stato = intermedio;
}
if(mouseX > 130 && mouseX < 160 && mouseY > 50 && mouseY <70){
c = color(130, 0, 255);
stato = intermedio;
}
}
if (stato == intermedio){
if(mouseX > 100 && mouseX < 150 && mouseY > 100 && mouseY < 120){
stato = play;
client.write(nome+c);
}
}
}
Answers
Moved because "Using Processing" is about questions about the PDE and the installation.
"does not work (the name is written on the screen and not saved)"
On which side? What do you mean by "save in a variable"? Which variable?
my goal is to save your Nickname in a variable. The problem is that the client needs to go to the server name (nickname) but do not know how to do
up :(
double-clicking on the client I run the client opens two windows, but on the server recognizes them as if they were the same client and not two separate, why?: (
//CLIENT