Merge pull request #2466 from kfeuz/Issue2133

Adds disconnectEvent() to Server... merging and will make additions
This commit is contained in:
Ben Fry
2014-04-19 04:37:00 -04:00
2 changed files with 15 additions and 5 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
}
}
@@ -140,7 +151,7 @@ public class Client implements Runnable {
* @usage application
*/
public void stop() {
if (disconnectEventMethod != null) {
if (disconnectEventMethod != null && thread != null){
try {
disconnectEventMethod.invoke(parent, new Object[] { this });
} catch (Exception e) {

View File

@@ -109,8 +109,7 @@ public class Server implements Runnable {
* @param client the client to disconnect
*/
public void disconnect(Client client) {
//client.stop();
client.dispose();
client.stop();
int index = clientIndex(client);
if (index != -1) {
removeIndex(index);
@@ -216,8 +215,8 @@ public class Server implements Runnable {
thread = null;
if (clients != null) {
for (int i = 0; i < clientCount; i++) {
disconnect(clients[i]);
while(clientCount>0){
disconnect(clients[0]);
}
clientCount = 0;
clients = null;