Storing a changing string?

edited August 2014 in Programming Questions

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

  • edited August 2014

    ur client code can't run and you need to use Ctrl+k to format code properly

  • Answer ✓

    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)

        Messages m = new Messages(25,500);
        ArrayList<Messages> messages;
        String typing = "";
    
        void setup() {
          background(255);
          size(displayWidth,displayHeight);
    
           for (int i = messages.size()-1; i >= 0; i--) {
            Messages messages = messages.get(i);
            m.write();
          }
    
        m.write();
        }
    
        class Messages {
          float xpos, ypos;
          Messages(float xpos, float ypos) {
          xpos = xpos;
          ypos = ypos;
          } 
          void write() {
            text(typing,xpos,ypos);
            messages.add(new Messages(xpos,ypos));
          }
        }
    

    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

  • edited August 2014

    StringList or ArrayList<String>

    example:

        ArrayList<String> namelist;
        namelist = new ArrayList<String>();
        namelist.add("hello");  
        text(namelist.get(0),10,10); 
    

    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

    1. Declare a variable to refer to the ArrayList: ArrayList<Message> messages;
    2. Instantiate a new ArrayList and assign its reference to the variable: messages = new ArrayList();
    3. Now we can add() new objects to the ArrayList via its reference variable + dot operator:
      messages.add( new Message(25, 500) ); http://processing.org/reference/dot.html
    • You create an array list, but doesn't initialize it, so you cannot do anything with it. See item 2 of GoToLoop.
    • In the loop in setup(), you create a variable of same name than the global messages variable, which will confuse both the compiler (hence your message) and the reader...
    • You try to get items before putting them in the list.
    • The class should be named Message and should hold only one message. It should not change the global messages variable. It is just a container for the information you want to store.
  • Thank you both so much this really helped. I'm new to using Arrays and the likes, obviously. Sorry for getting it all completely wrong

Sign In or Register to comment.