Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : How do I send data to only one client (using the network library)? [SOLVED!]

$
0
0
If you look at implementation of the write() method, you will see that all it does, just forwards the data to the Client.write().

So in order to write to specific client, all you have to do is to save your clients when your sketch is notified via serverEvent()  

and then just do simple mySavedClient.write("My data"); here's short snippet

Client myLastClient;
void serverEvent(Server someServer, Client someClient) {
  println("We have a new client: " + someClient.ip());
  myLastClient = someClient;
}
void draw(){
  if ( myLastClient != null ){
         println("We only write to the last connected client with ip: "+ myLastClient.ip());
         myLastClient.write("You're still our last client");
   }
}





This is code from Server.write(String) method:

  public void write(String data) {
    int index = 0;
    while (index < clientCount) {
      clients[index].write(data);
      if (clients[index].active()) {
        index++;
      } else {
        removeIndex(index);
      }
    }
  }

Could be found on github here

Viewing all articles
Browse latest Browse all 1768

Trending Articles