We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Processing as a web server
Page Index Toggle Pages: 1
Processing as a web server (Read 559 times)
Processing as a web server
Jan 11th, 2009, 12:07am
 
This is probably a real newb question but because of the nature of it it's very difficult to search for it in the forums and find an answer.

Is it possible to use Processing as a simple web server, serving up HTML?

I've just been tinkering with the Server object and its write() function. I seem to be able to connect to it but I don't seem to be able get it to output HTML (or anything for that matter).

What am I missing?
Re: Processing as a web server
Reply #1 - Jan 11th, 2009, 8:43am
 
sure, but you'd have to implement http (or at lease some minimal portion of it) for it to be able to speak to browsers:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

Code:

import processing.net.*;

String HTTP_GET_REQUEST = "GET /";
String HTTP_HEADER = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";

Server s;
Client c;
String input;

void setup()
{
s = new Server(this, 8080); // start server on http-alt
}

void draw()
{
// Receive data from client
c = s.available();
if (c != null) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // Only up to the newline

if (input.indexOf(HTTP_GET_REQUEST) == 0) // starts with ...
{
c.write(HTTP_HEADER); // answer that we're ok with the request and are gonna send html

// some html
c.write("<html><head><title>Processing talkin'</title></head><body><h3>Your base are belong to us!");
c.write("</h3></body></html>");

// close connection to client, otherwise it's gonna wait forever
c.stop();
}
}
}


F
Re: Processing as a web server
Reply #2 - Jan 11th, 2009, 6:03pm
 
Thanks fjen - had a funny feeling that might be the case.

Not sure I've got the energy (or wherewithal for that matter) to code enough of the stack to make it useful. Has anyone managed to embed a very lightweight webserver (Ruby for example) into Processing to serve up content?

There seems to be some documentation on embedding Processing into another Java environment but not the other way round.
Re: Processing as a web server
Reply #3 - Jan 11th, 2009, 8:00pm
 
yes i wouldn't either. what are you trying to do? i don't see any reason why you would want to use processing as a webserver when there are such things as apache available for free ..?

F
Re: Processing as a web server
Reply #4 - Jan 12th, 2009, 7:24am
 
hi,
i have been playing with using processing as a web server a while back and just took a look at it, maybe you want to give it a try. i packaged it as a library, so the .zip content goes into the libraries folder. i included 1 example, a server that runs on port 8080. when you run the sketch, you can access the server in your browser with

http://localhost:8080/index.html

http://localhost:8080/image.html

things you can do with it: send requested files from the server to the browser, compile your own html files on the fly, queries - i havnt finished sending e.g. PImages or PGraphics back to a browser but thats actually why i started to write this library.
anyway, you can download an experimental version at
http://www.sojamo.de/libraries/http/sHTTP.zip

best,
andreas
Re: Processing as a web server
Reply #5 - Jan 12th, 2009, 1:08pm
 
You could also write a servlet that uses processing to create the image.

You could then deploy it on Tomcat(http://tomcat.apache.org/) or Jetty (http://www.mortbay.org/jetty/).

I have used PGrphics3D successfully on the server side. This is not a simple task for beginners though.

Re: Processing as a web server
Reply #6 - Jan 12th, 2009, 1:33pm
 
The reason I ask is that I'm planning on putting together a couple of sensors to talk to an Arduino. I'd like to be able to store the data that they're measuring (probably on the Arduino with an SD card).

I then want to be able to run some analysis on the data, including graphing, and interrogate using a web browser. I'm expecting to have multiple pages and images.
Re: Processing as a web server
Reply #7 - Jan 12th, 2009, 9:16pm
 
i don't see the need for processing as a server there. you should check out the arduino ethershield, with it you can post the values directly to some server anywhere on the net. have some scripts to store and retrive the data from a database and maybe an applet to visualize it on the client side.
Re: Processing as a web server
Reply #8 - Jan 12th, 2009, 11:09pm
 
Thanks for the advice fjen - I've basically been coming to the same conclusions over the last couple of days.
Page Index Toggle Pages: 1