Skip to content

Commit 3dfc835

Browse files
author
Kristijan Sedlak
committed
updating WebSocketClient and WebSocketServer onClose & onIOError events to simulate browser WebSocket behaviour
1 parent 986a0e1 commit 3dfc835

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.Set;
1515
import java.util.concurrent.LinkedBlockingQueue;
1616

17+
import android.util.Log;
18+
1719
/**
1820
* The <tt>WebSocketClient</tt> is an abstract class that expects a valid
1921
* "ws://" URI to connect to. When connected, an instance recieves important
@@ -116,12 +118,16 @@ public void connect() {
116118
* closes the socket connection, and ends the client socket thread.
117119
* @throws IOException When socket related I/O errors occur.
118120
*/
119-
public void close() throws IOException {
120-
if (running) {
121-
this.running = false;
122-
selector.wakeup();
123-
conn.close();
124-
}
121+
public void close() throws IOException
122+
{
123+
Boolean active = running;
124+
this.running = false;
125+
126+
if (active)
127+
{
128+
selector.wakeup();
129+
conn.close();
130+
}
125131
}
126132

127133
/**
@@ -344,17 +350,36 @@ public void onOpen(WebSocket conn) {
344350
* Calls subclass' implementation of <var>onClose</var>.
345351
* @param conn
346352
*/
347-
public void onClose(WebSocket conn) {
348-
onClose();
353+
public void onClose(WebSocket conn)
354+
{
355+
try
356+
{
357+
close();
358+
}
359+
catch (IOException ex)
360+
{
361+
onIOError(ex);
362+
}
363+
onClose();
349364
}
350365

351366
/**
352367
* Triggered on any IOException error. This method should be overridden for custom
353368
* implementation of error handling (e.g. when network is not available).
354369
* @param ex
355370
*/
356-
public void onIOError(IOException ex) {
371+
public void onIOError(IOException ex)
372+
{
357373
ex.printStackTrace();
374+
375+
try
376+
{
377+
this.close();
378+
}
379+
catch (IOException e)
380+
{
381+
onIOError(ex);
382+
}
358383
}
359384

360385
// ABTRACT METHODS /////////////////////////////////////////////////////////

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,19 @@ public void onClose(WebSocket conn) {
420420
* implementation of error handling (e.g. when network is not available).
421421
* @param ex
422422
*/
423-
public void onIOError(IOException ex) {
424-
ex.printStackTrace();
425-
}
423+
public void onIOError(IOException ex)
424+
{
425+
ex.printStackTrace();
426+
427+
try
428+
{
429+
this.stop();
430+
}
431+
catch (IOException e)
432+
{
433+
onIOError(ex);
434+
}
435+
}
426436

427437
private byte[] getPart(String key) {
428438
long keyNumber = Long.parseLong(key.replaceAll("[^0-9]",""));

0 commit comments

Comments
 (0)