Created
August 27, 2012 07:36
-
-
Save gourneau/3486508 to your computer and use it in GitHub Desktop.
ARTHttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //a ugly http server, the worst ever made | |
| // Learning Processing | |
| // Daniel Shiffman | |
| // http://www.learningprocessing.com | |
| // Example 19-1: Simple therapy server | |
| // 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(); | |
| // We should only proceed if the client is not null | |
| if (client!= null) { | |
| server.write("HTTP/1.0 200 OK\r\n"); | |
| server.write("Server: BroNet\r\n"); | |
| server.write("Content-Type: text/html\r\n"); | |
| server.write("\r\n"); | |
| // Receive the message | |
| // The message is read using readString(). | |
| client.readBytes(); | |
| client.stop(); | |
| // Reset newMessageColor to black | |
| newMessageColor = 0; | |
| } | |
| } | |
| // The serverEvent function is called whenever a new client connects. | |
| void serverEvent(Server server, Client client) { | |
| //client.readBytes(); | |
| println(incomingMessage); | |
| // Reset newMessageColor to black | |
| newMessageColor = 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment