Skip to content

Commit 252a68e

Browse files
committed
Implements disconnectEvent for the Server code as requested in Issue processing#2133
1 parent c119388 commit 252a68e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,24 @@ public Client(PApplet parent, String host, int port) {
118118
* @throws IOException
119119
*/
120120
public Client(PApplet parent, Socket socket) throws IOException {
121+
this.parent = parent;
121122
this.socket = socket;
122123

123124
input = socket.getInputStream();
124125
output = socket.getOutputStream();
125126

126127
thread = new Thread(this);
127128
thread.start();
129+
130+
// reflection to check whether host sketch has a call for
131+
// public void disconnectEvent(processing.net.Client)
132+
try {
133+
disconnectEventMethod =
134+
parent.getClass().getMethod("disconnectEvent",
135+
new Class[] { Client.class });
136+
} catch (Exception e) {
137+
// no such method, or an error.. which is fine, just ignore
138+
}
128139
}
129140

130141

java/libraries/net/src/processing/net/Server.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ public Server(PApplet parent, int port) {
109109
* @param client the client to disconnect
110110
*/
111111
public void disconnect(Client client) {
112-
//client.stop();
112+
//Calling client.stop() here would cause duplicate
113+
//calls to disconnectEvent in the containing sketch,
114+
//once for the stop() and once for the terminated connection.
115+
//Instead just dispose of the client and let the terminated
116+
//connection generate the disconnectEvent message;
113117
client.dispose();
114118
int index = clientIndex(client);
115119
if (index != -1) {

0 commit comments

Comments
 (0)