IAT267 Simple Chat Server Example.docx

3 Pages
Unlock Document

Interactive Arts & Tech
IAT 267
Helmine Serban

Simple Chat Server Server Code // Import the net libraries 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(); //returns the next client that has a new message // 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( "Thank you for " + incomingMessage + "!\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.
More Less

Related notes for IAT 267

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.