Loading...
Logo
Processing Forum
Hi

I'm trying to start using javascript to code sketches with user interfaces on the web (ok, it is a game). I have no experience in networks so i just tried simple examples from the books of Reas-Freys, but I can't make them work. I tried the following code:


Copy code

  1. import processing.net.*;

    Server server;


    void setup() {
      size(400, 200);
      println("test");
      server = new Server(this, 5204);
    }

    void draw() {
      background(255);

      println("test");

      Client client = server.available();
    }



The result is that "test" is only displayed once and firefox console displays

[07:39:24,327] uncaught exception: Processing.js: Unable to execute pjs sketch: ReferenceError: Server is not defined

Also the sketch log displays "Server started: http://127.0.0.1:59095", which is clearly not 5204 (i tried other ports).

Where is the goofy mistake?

Replies(5)

This will go in the Technical FAQ soon, I think...
If you use the import statement in a sketch, then you cannot run it in JavaScript mode.
Simple and definitive.
That's why several old sketches using the previous default Java imports of Processing stopped working, because now we have to be explicit about them: these Java features are not available in JS.

ReferenceError: Server is not defined
It means Server class doesn't exist!
import processing.net.*;
Java libraries don't work in JavaScript mode. You gotta find 1 written in JS.
Server started: http://127.0.0.1:59095, which is clearly not 5204
That is a local server created by Processing IDE in JavaScript Mode in order to run it.
Ok thanks for your answers. Then it is funny that the program runs until displaying "test" although the non-existing Server class is used before.

So how does everybody else use servers with processing.js? Would you recommand some documentation?
In JavaScript, communication with servers is usally done with AJAX.

So, you might combine Processing.js with one of the AJAX libraries, such as jQuery, YUI library etc.


Here's an example of how processingJS can be mixed with jQuery