From 1174d7e8751b7b572420b4fbd1ac4e54d1df3a03 Mon Sep 17 00:00:00 2001 From: Peter Foley Date: Tue, 2 Apr 2013 10:12:11 -0400 Subject: [PATCH] add isConnected method 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. --- .../android_websockets/WebSocketClient.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/com/codebutler/android_websockets/WebSocketClient.java b/src/com/codebutler/android_websockets/WebSocketClient.java index 3b14a9f..82aadbe 100644 --- a/src/com/codebutler/android_websockets/WebSocketClient.java +++ b/src/com/codebutler/android_websockets/WebSocketClient.java @@ -36,6 +36,7 @@ public class WebSocketClient { private Handler mHandler; private List mExtraHeaders; private HybiParser mParser; + private boolean mConnected; private final Object mSendLock = new Object(); @@ -47,8 +48,9 @@ public static void setTrustManagers(TrustManager[] tm) { public WebSocketClient(URI uri, Listener listener, List extraHeaders) { mURI = uri; - mListener = listener; + mListener = listener; mExtraHeaders = extraHeaders; + mConnected = false; mParser = new HybiParser(this); mHandlerThread = new HandlerThread("websocket-thread"); @@ -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); @@ -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); @@ -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;