Skip to content

Commit daabf82

Browse files
author
Kristijan Sedlak
committed
implementing onIOError method for handling IOException errors
1 parent 83f4a5c commit daabf82

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Writing your own WebSocket Client
4747

4848
The `net.tootallnate.websocket.WebSocketClient` abstract class can connect to
4949
valid WebSocket servers. The constructor expects a valid `ws://` URI to
50-
connect to. Important events `onOpen`, `onClose`, and `onMessage` get fired
51-
throughout the life of the WebSocketClient, and must be implemented in **your**
52-
subclass.
50+
connect to. Important events `onOpen`, `onClose`, `onMessage` and onIOError get
51+
fired throughout the life of the WebSocketClient, and must be implemented in
52+
**your** subclass.
5353

5454
License
5555
-------

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private boolean tryToConnect(InetSocketAddress remote) {
150150
client.register(selector, SelectionKey.OP_CONNECT);
151151

152152
} catch (IOException ex) {
153-
ex.printStackTrace();
153+
onIOError(ex);
154154
return false;
155155
}
156156

@@ -180,7 +180,7 @@ public void run() {
180180
}
181181
}
182182
} catch (IOException ex) {
183-
ex.printStackTrace();
183+
onIOError(ex);
184184
} catch (NoSuchAlgorithmException ex) {
185185
ex.printStackTrace();
186186
}
@@ -348,6 +348,15 @@ public void onClose(WebSocket conn) {
348348
onClose();
349349
}
350350

351+
/**
352+
* Triggered on any IOException error. This method should be overridden for custom
353+
* implementation of error handling (e.g. when network is not available).
354+
* @param ex
355+
*/
356+
public void onIOError(IOException ex) {
357+
ex.printStackTrace();
358+
}
359+
351360
// ABTRACT METHODS /////////////////////////////////////////////////////////
352361
public abstract void onMessage(String message);
353362
public abstract void onOpen();

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void run() {
196196
selector = Selector.open();
197197
server.register(selector, server.validOps());
198198
} catch (IOException ex) {
199-
ex.printStackTrace();
199+
onIOError(ex);
200200
return;
201201
}
202202

@@ -250,7 +250,7 @@ public void run() {
250250
}
251251
}
252252
} catch (IOException ex) {
253-
ex.printStackTrace();
253+
onIOError(ex);
254254
} catch (RuntimeException ex) {
255255
ex.printStackTrace();
256256
} catch (NoSuchAlgorithmException ex) {
@@ -415,6 +415,15 @@ public void onClose(WebSocket conn) {
415415
}
416416
}
417417

418+
/**
419+
* Triggered on any IOException error. This method should be overridden for custom
420+
* implementation of error handling (e.g. when network is not available).
421+
* @param ex
422+
*/
423+
public void onIOError(IOException ex) {
424+
ex.printStackTrace();
425+
}
426+
418427
private byte[] getPart(String key) {
419428
long keyNumber = Long.parseLong(key.replaceAll("[^0-9]",""));
420429
long keySpace = key.split("\u0020").length - 1;

0 commit comments

Comments
 (0)