We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there! I'm working on a client application and a server application that allows two or more clients to message each other. At the moment I can send a typed message from one client, through the server, and read back out to the client, but when the second client replies the original message is lost.
So I was wondering if there was a way to store the original message (possibly using an array?) and then store the new message in the same array? I'm sorry if this is explained really badly, I'll try and be a little clearer. I'm trying to send a message from one client to another, and allow the second client to reply. However, I would also like to be able to show the previous messages on both client's screens? Is there any way to do this? I'll paste the code below
Client Code:
import processing.net.*;
// Declare a client
Client client;
int currentScreen;
// 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(displayWidth,displayHeight-200);
noStroke();
client = new Client(this,"192.168.0.7", 12345);
f = createFont("courier new",16,true);
}
void draw() {
background(255,215,0);
switch(currentScreen) {
case 0: drawScreenZero(); break;
case 1: drawScreenOne(); break;
}
void drawScreenZero() {
}
void drawScreenOne() {
textFont(f);
text(typing,25,displayHeight-260);
if (client.available() > 0) {
// Read it as a String
messageFromServer = client.readString();
// Set brightness to 0
newMessageColor = 0;
fill(0);
textFont(f);
text(messageFromServer,width/2,140);
}
}
void mousePressed() {
if (mouseX < 20) { currentScreen++; }
if (currentScreen > 2) { currentScreen = 0; }
if (mouseX > displayWidth-510 && mouseX < displayWidth-380 && mouseY > displayHeight-290 && mouseY < displayHeight-160) {
text(typing,width/2,80);
client.write(typing); // When the user hits enter, the String typed is sent to the Server.
typing = "";
}
}
void keyPressed() {
// If the return key is pressed, save the String and clear it
if (key == '\n' && currentScreen >= 1) {
// When the user hits enter, write the sentence out to the Server
text(typing,width/2,80);
client.write(typing); // When the user hits enter, the String typed is sent to the Server.
typing = "";
}
if (keyCode == BACKSPACE && currentScreen >= 1) {
if (typing.length() > 0 && currentScreen >= 1) {
typing = typing.substring(0, typing.length()-1);
}
} else if (keyCode == DELETE && currentScreen >= 1) {
typing = "";
} else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && currentScreen >= 1) {
typing = typing + key;
}
}
void serverEvent(Server server, Client client) {
println("A new client has connected: " + client.ip());
}
}
Server Code:
import processing.net.*;
// Declare a server
Server server;
// Used to indicate a new message has arrived
float newMessageColor = 255;
PFont f;
String incomingMessage = "";
String user = "";
void setup() {
size(400,200);
// Create the Server on port 5204
server = new Server(this, 12345);
f = createFont("Arial",16,true);
}
void draw() {
background(newMessageColor);
// newMessageColor fades to white over time
newMessageColor = 0;
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();
user = 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( incomingMessage ); // 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;
}
I hope somebody out there can help, I've been trying to fix this for ages! Thanks in advance :)
Answers
ur client code can't run and you need to use Ctrl+k to format code properly
You can use an ArrayList to store the incoming messages: it will grow as they arrive, so you can easily keep the history of the exchanges.
Thank you! But wouldn't I need a class to do that? I'd have no idea how to write the class
What do you mean you'd need a class to do that? Have you tried it?
I tried setting up an ArrayList but it said I needed a class. So I tried writing a class for it and well...I have no idea how to write a class that'd do what I want it to
What was the exact error? Do you have an MCVE?
Sorry about this! The message I get is "The function get(int) does not exist". I'll post you the code (shortened this time, oops)
I should probably point out that this is the client code, I really don't know if I've got any of this right but I was hoping somebody might be able to help
StringList or ArrayList<String>
example:
I don't actually know the difference between them but u can use all the same Methods on the two ArrayList Methods listed here: https://www.processing.org/reference/StringList.html
I suggest you to read the ArrayList's reference: http://processing.org/reference/ArrayList.html
ArrayList<Message> messages;
new
ArrayList and assign its reference to the variable:messages = new ArrayList();
new
objects to the ArrayList via its reference variable + dot operator:messages.add( new Message(25, 500) );
http://processing.org/reference/dot.htmlThank you both so much this really helped. I'm new to using Arrays and the likes, obviously. Sorry for getting it all completely wrong