'serverEvent' operates within the time of performing the delay in 'draw()'

import processing.net.*;

int port = 10002;
Server myServer;
long COUNT_NUM = 0;

void setup()
{
size(400, 400);
background(0);
myServer = new Server(this, port);
}

void draw() {
background(255);
delay(10000);
}

void serverEvent(Server someServer, Client someClient) {
COUNT_NUM++;
Client thisClient = myServer.available();
if (thisClient !=null) {
String whatClientSaid = thisClient.readString();
if (whatClientSaid != null) {

  println(" COUNT : " + str(COUNT_NUM) +" / " + whatClientSaid);
  
} 

}
}

A1

‘serverEvent’ operates within the time of performing the delay in ‘draw()’.

What is the principle?

Is it a thread?

And how do you do this? Can it be implemented through threads?

Yes, serverEvent() is called back by Server’s thread. :thread:

1 Like

Dear @GoToLoop

Can’t we make it like’serverEvent’?

There is a delay in’draw()’ when loading photo data, etc., or doing something. At this time,

When sending data processing to the thread, it seems that the’delay’ of’draw()’ will be resolved.

Is there any good way or example?

Hello,

I am only sharing partial code…

Visual Example on YouTube:
Arduino to Processing Server to Processing Client

  1. Arduino is sending data to Processing Server.
  2. Processing Server is only receiving data from Arduino and sending to Processing Client.
    draw() is not looping; see noLoop() in setup().
Processing Client Code
import processing.serial.*;

Serial myPort;
String sRx;

import processing.net.*;

Server s;
String input;

void setup() 
  {
  size(200, 200);
  background(0);
  
  s = new Server(this, 12345);
  
  printArray(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 19200);
  myPort.bufferUntil(' ');
  frameRate(60);
  noLoop();
  }

void draw() 
  {
//  println(sRx);
  }

  void serialEvent(Serial myPort) 
  {
  sRx = myPort.readString();
  s.write(sRx);
//  println(sRx);
  }
  1. Processing Client is receiving data and buffer it into an array and updates canvas every draw() cycle (60fps default).

References:
https://processing.org/tutorials/network/
https://processing.org/reference/libraries/net/index.html
Examples that come with Processing:

image

:)