processing / processing-docs

[Deprecated] Processing reference, examples, tutorials, and website
https://processing.org
371 stars 179 forks source link

Network library clientEvent() example incorrectly drawing outside draw() #801

Closed jeremydouglass closed 4 years ago

jeremydouglass commented 4 years ago

Issue description

The Network library clientEvent() example incorrectly attempts drawing outside draw(). This causes elements to fail to render. See previous discussion:

https://github.com/processing/processing/issues/5992#issuecomment-596142827

URL(s) of affected page(s)

https://processing.org/reference/libraries/net/clientEvent_.html

Proposed fix

The example should be updated, moving all drawing from clientEvent() to draw().

REAS commented 4 years ago

I think this does the job:

import processing.net.*;

Client myClient;
int dataIn;

void setup() {
  size(200, 200);
  myClient = new Client(this, "127.0.0.1", 5204);
  noLoop();
}

void draw() { 
  background(dataIn);
}

// ClientEvent message is generated when the server 
// sends data to an existing client.
void clientEvent(Client someClient) {
  print("Server Says:  ");
  dataIn = someClient.read();
  println(dataIn);
  redraw();
}