Simple client server interaction

Hi guys,
making my way through the processing book and got to the client server chapter which is fantastic.
I haven’t gone as far as Daniel has suggested by creating a pong game but close enough. I am moving a rect around the client from the server. However, my question is I have code to move the rect, but what is the best course of action for stopping the box once a key is released? As it stands, the box continues to move.
I have tried keyReleased() to send a ‘stop’ int but clearly this is called after I release a button, meaning the rect doesn’t move :).

Thank you.

1 Like

i am not sure your question has anything to do with client server projects

for a keyboard operation you can use
https://processing.org/reference/keyPressed_.html
start move
https://processing.org/reference/keyReleased_.html
stop move
OR
while https://processing.org/reference/keyPressed.html move

What does your server code look like? It is unclear from your description if you are trying to add keyReleased to your client or server sketch. Client can use IO functions, server receives signals.

I did a quick exploration of this.

In the Processing examples…

Modify “SharedCanvasServer” as follows:

int count;
void keyReleased()
{
count++;
s.write(0 + " " + 1 + " " + 2 + " " + count + “\n”);
println(“keyReleased Server”);
// text(“keyReleased”, 100, 100);
}

Modify “SharedCanvasClient” as follows:

if (data[0]==0 &data[1]==1&data[2]==2)
  {
  println(data[3]);
  println("keyReleased");
  }
else  
  line(data[0], data[1], data[2], data[3]);

When you release a key it will increment count and display on console:

keyReleased Server
1
keyReleased
keyReleased Server
2
keyReleased
keyReleased Server
3
keyReleased
keyReleased Server
4
keyReleased

Note:
Only one console is active so this is why “keyReleased Server” shows up on same console as client.
You must start “server” and then “client” for this to work.
The server draw window must be “active” to accept a keypress and release.
The client and server examples worked for me using localhost IP of 127.0.0.1
:slight_smile:

1 Like