Skip to content

Commit 8d3909f

Browse files
author
Kristijan Sedlak
committed
removing android imports, adding onIOError abstract method
1 parent 52c1cd1 commit 8d3909f

File tree

4 files changed

+39
-51
lines changed

4 files changed

+39
-51
lines changed

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.nio.ByteBuffer;
66
import java.nio.channels.NotYetConnectedException;
77
import java.nio.channels.SocketChannel;
8-
import java.nio.charset.Charset;
98
import java.security.NoSuchAlgorithmException;
109
import java.util.concurrent.BlockingQueue;
1110

src/net/tootallnate/websocket/WebSocketClient.java

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

17-
import android.util.Log;
18-
1917
/**
2018
* The <tt>WebSocketClient</tt> is an abstract class that expects a valid
2119
* "ws://" URI to connect to. When connected, an instance recieves important
@@ -133,12 +131,28 @@ public void close() throws IOException
133131
* @throws IOException When socket related I/O errors occur.
134132
*/
135133
public void send(String text) throws IOException {
136-
if (conn != null && client.isConnected())
134+
if (conn != null)
137135
{
138136
conn.send(text);
139137
}
140138
}
141139

140+
/**
141+
* Reinitializes and prepares the class to be used for reconnect.
142+
* @return
143+
*/
144+
public void releaseAndInitialize()
145+
{
146+
conn = null;
147+
client = null;
148+
selector = null;
149+
running = false;
150+
draft = null;
151+
number1 = 0;
152+
number2 = 0;
153+
key3 = null;
154+
}
155+
142156
private boolean tryToConnect(InetSocketAddress remote) {
143157
// The WebSocket constructor expects a SocketChannel that is
144158
// non-blocking, and has a Selector attached to it.
@@ -154,7 +168,7 @@ private boolean tryToConnect(InetSocketAddress remote) {
154168
client.register(selector, SelectionKey.OP_CONNECT);
155169

156170
} catch (IOException ex) {
157-
onIOError(ex);
171+
onIOError(conn, ex);
158172
return false;
159173
}
160174

@@ -184,7 +198,7 @@ public void run() {
184198
}
185199
}
186200
} catch (IOException ex) {
187-
onIOError(ex);
201+
onIOError(conn, ex);
188202
} catch (NoSuchAlgorithmException ex) {
189203
ex.printStackTrace();
190204
}
@@ -354,38 +368,23 @@ public void onClose(WebSocket conn)
354368
{
355369
onClose();
356370
}
357-
358-
conn = null;
359-
client = null;
360-
selector = null;
361-
running = false;
362-
draft = null;
363-
number1 = 0;
364-
number2 = 0;
365-
key3 = null;
371+
372+
releaseAndInitialize();
366373
}
367374

368375
/**
369-
* Triggered on any IOException error. This method should be overridden for custom
370-
* implementation of error handling (e.g. when network is not available).
371-
* @param ex
376+
* Calls subclass' implementation of <var>onIOError</var>.
377+
* @param conn
372378
*/
373-
public void onIOError(IOException ex)
374-
{
375-
ex.printStackTrace();
376-
377-
try
378-
{
379-
this.close();
380-
}
381-
catch (IOException e)
382-
{
383-
onIOError(ex);
384-
}
385-
}
379+
public void onIOError(WebSocket conn, IOException ex)
380+
{
381+
releaseAndInitialize();
382+
onIOError(ex);
383+
}
386384

387385
// ABTRACT METHODS /////////////////////////////////////////////////////////
388386
public abstract void onMessage(String message);
389387
public abstract void onOpen();
390388
public abstract void onClose();
389+
public abstract void onIOError(IOException ex);
391390
}

src/net/tootallnate/websocket/WebSocketListener.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ interface WebSocketListener {
4646
* @param conn The <tt>WebSocket</tt> instance this event is occuring on.
4747
*/
4848
public void onClose(WebSocket conn);
49-
49+
50+
/**
51+
* Triggered on any IOException error. This method should be overridden for custom
52+
* implementation of error handling (e.g. when network is not available).
53+
* @param ex
54+
*/
55+
public void onIOError(IOException ex);
56+
5057
/**
5158
* Called to retrieve the Draft of this listener.
5259
*/

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -415,25 +415,6 @@ 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-
{
425-
ex.printStackTrace();
426-
427-
try
428-
{
429-
this.stop();
430-
}
431-
catch (IOException e)
432-
{
433-
onIOError(ex);
434-
}
435-
}
436-
437418
private byte[] getPart(String key) {
438419
long keyNumber = Long.parseLong(key.replaceAll("[^0-9]",""));
439420
long keySpace = key.split("\u0020").length - 1;
@@ -450,4 +431,6 @@ private byte[] getPart(String key) {
450431
public abstract void onClientOpen(WebSocket conn);
451432
public abstract void onClientClose(WebSocket conn);
452433
public abstract void onClientMessage(WebSocket conn, String message);
434+
public abstract void onIOError(IOException ex);
435+
453436
}

0 commit comments

Comments
 (0)