Skip to content

Commit

Permalink
add isConnected method
Browse files Browse the repository at this point in the history
I converted my application from AutobahnAndroid to android-websockets
to take advantage of ssl support.
android-websockets did not have a isConnected method, which is used in
my application, so I implemented one.
  • Loading branch information
pefoley2 committed Apr 4, 2013
1 parent 878842d commit 1174d7e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/com/codebutler/android_websockets/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class WebSocketClient {
private Handler mHandler;
private List<BasicNameValuePair> mExtraHeaders;
private HybiParser mParser;
private boolean mConnected;

private final Object mSendLock = new Object();

Expand All @@ -47,8 +48,9 @@ public static void setTrustManagers(TrustManager[] tm) {

public WebSocketClient(URI uri, Listener listener, List<BasicNameValuePair> extraHeaders) {
mURI = uri;
mListener = listener;
mListener = listener;
mExtraHeaders = extraHeaders;
mConnected = false;
mParser = new HybiParser(this);

mHandlerThread = new HandlerThread("websocket-thread");
Expand Down Expand Up @@ -119,17 +121,21 @@ public void run() {

mListener.onConnect();

mConnected = true;

// Now decode websocket frames.
mParser.start(stream);

} catch (EOFException ex) {
Log.d(TAG, "WebSocket EOF!", ex);
mListener.onDisconnect(0, "EOF");
mConnected = false;

} catch (SSLException ex) {
// Connection reset by peer
Log.d(TAG, "Websocket SSL error!", ex);
mListener.onDisconnect(0, "SSL");
mConnected = false;

} catch (Exception ex) {
mListener.onError(ex);
Expand All @@ -147,6 +153,7 @@ public void run() {
try {
mSocket.close();
mSocket = null;
mConnected = false;
} catch (IOException ex) {
Log.d(TAG, "Error while disconnecting", ex);
mListener.onError(ex);
Expand All @@ -164,6 +171,10 @@ public void send(byte[] data) {
sendFrame(mParser.frame(data));
}

public boolean isConnected() {
return mConnected;
}

private StatusLine parseStatusLine(String line) {
if (TextUtils.isEmpty(line)) {
return null;
Expand Down

0 comments on commit 1174d7e

Please sign in to comment.