Implements disconnectEvent for the Server code as requested in Issue #2133

This commit is contained in:
Kyle Feuz
2014-04-18 01:42:54 -07:00
parent 17fe76c153
commit 0b50a98219
2 changed files with 16 additions and 1 deletions

View File

@@ -118,6 +118,7 @@ public class Client implements Runnable {
* @throws IOException
*/
public Client(PApplet parent, Socket socket) throws IOException {
this.parent = parent;
this.socket = socket;
input = socket.getInputStream();
@@ -125,6 +126,16 @@ public class Client implements Runnable {
thread = new Thread(this);
thread.start();
// reflection to check whether host sketch has a call for
// public void disconnectEvent(processing.net.Client)
try {
disconnectEventMethod =
parent.getClass().getMethod("disconnectEvent",
new Class[] { Client.class });
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
}
}

View File

@@ -109,7 +109,11 @@ public class Server implements Runnable {
* @param client the client to disconnect
*/
public void disconnect(Client client) {
//client.stop();
//Calling client.stop() here would cause duplicate
//calls to disconnectEvent in the containing sketch,
//once for the stop() and once for the terminated connection.
//Instead just dispose of the client and let the terminated
//connection generate the disconnectEvent message;
client.dispose();
int index = clientIndex(client);
if (index != -1) {