File tree Expand file tree Collapse file tree 3 files changed +25
-7
lines changed
src/net/tootallnate/websocket Expand file tree Collapse file tree 3 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -47,9 +47,9 @@ Writing your own WebSocket Client
4747
4848The ` net.tootallnate.websocket.WebSocketClient ` abstract class can connect to
4949valid 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
5454License
5555-------
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments