From e1877d697ca25e15392b9012a907e0e96e6034a2 Mon Sep 17 00:00:00 2001 From: Vinay S Shenoy Date: Mon, 20 May 2013 22:02:27 +0530 Subject: [PATCH 1/2] Added support for Endpoints --- .gitignore | 1 + .../android_websockets/SocketIOClient.java | 117 ++++++++++++++---- 2 files changed, 93 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 926c55d..e66abab 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ gen .classpath .project local.properties +/.settings diff --git a/src/com/codebutler/android_websockets/SocketIOClient.java b/src/com/codebutler/android_websockets/SocketIOClient.java index 21e2593..58e2268 100644 --- a/src/com/codebutler/android_websockets/SocketIOClient.java +++ b/src/com/codebutler/android_websockets/SocketIOClient.java @@ -18,6 +18,7 @@ import android.net.http.AndroidHttpClient; import android.os.Looper; +import android.text.TextUtils; import android.util.Log; public class SocketIOClient { @@ -36,16 +37,28 @@ public static interface Handler { } private static final String TAG = "SocketIOClient"; - + String mURL; Handler mHandler; String mSession; int mHeartbeat; WebSocketClient mClient; + String mEndpoint; public SocketIOClient(URI uri, Handler handler) { - // remove trailing "/" from URI, in case user provided e.g. http://test.com/ - mURL = uri.toString().replaceAll("/$", "") + "/socket.io/1/"; + this(uri, handler, null); + } + + public SocketIOClient(URI uri, Handler handler, String namespace) { + mEndpoint = namespace; + + if (TextUtils.isEmpty(namespace)) { + mEndpoint = "socket.io"; + } + + // remove trailing "/" from URI, in case user provided e.g. + // http://test.com/ + mURL = uri.toString().replaceAll("/$", "") + "/" + mEndpoint + "/1/"; mHandler = handler; } @@ -54,8 +67,7 @@ private static String downloadUriAsString(final HttpUriRequest req) throws IOExc try { HttpResponse res = client.execute(req); return readToEnd(res.getEntity().getContent()); - } - finally { + } finally { client.close(); } } @@ -91,21 +103,21 @@ public void run() { } }); } - + public void emit(final String message) { mSendHandler.post(new Runnable() { - + @Override public void run() { mClient.send(String.format("3:::%s", message)); } }); } - + public void emit(final JSONObject jsonMessage) { - + mSendHandler.post(new Runnable() { - + @Override public void run() { mClient.send(String.format("4:::%s", jsonMessage.toString())); @@ -140,10 +152,10 @@ public void onMessage(String message) { // message final String messageId = parts[1]; final String dataString = parts[3]; - - if(!"".equals(messageId)) { + + if (!"".equals(messageId)) { mSendHandler.post(new Runnable() { - + @Override public void run() { mClient.send(String.format("6:::%s", messageId)); @@ -154,20 +166,20 @@ public void run() { break; } case 4: { - //json message + // json message final String messageId = parts[1]; final String dataString = parts[3]; - + JSONObject jsonMessage = null; - + try { jsonMessage = new JSONObject(dataString); - } catch(JSONException e) { + } catch (JSONException e) { jsonMessage = new JSONObject(); } - if(!"".equals(messageId)) { + if (!"".equals(messageId)) { mSendHandler.post(new Runnable() { - + @Override public void run() { mClient.send(String.format("6:::%s", messageId)); @@ -211,8 +223,7 @@ public void run() { default: throw new Exception("unknown code"); } - } - catch (Exception ex) { + } catch (Exception ex) { cleanup(); onError(ex); } @@ -252,7 +263,7 @@ public void disconnect() throws IOException { private void cleanup() { mClient.disconnect(); mClient = null; - + mSendLooper.quit(); mSendLooper = null; mSendHandler = null; @@ -284,12 +295,68 @@ public void run() { connectSession(); Looper.loop(); - } - catch (Exception e) { + } catch (Exception e) { mHandler.onError(e); } }; }.start(); } -} + /** + * Connect to an endpoint + */ + public void connectToEndpoint(final String endpoint) { + + if (mClient.isConnected() && !TextUtils.isEmpty(endpoint)) { + mEndpoint = endpoint; + mSendHandler.post(new Runnable() { + + @Override + public void run() { + mClient.send("1::" + endpoint); + } + }); + } + } + + /** + * Disconnect from an endpoint or socket + * + * @param endpoint + * {@code null} to disconnect the entire socket, otherwise the + * endpoint to disconnect from + */ + public void sendDisconnect(final String endpoint) { + + if (TextUtils.isEmpty(endpoint)) { + + mSendHandler.post(new Runnable() { + + @Override + public void run() { + mClient.send("0"); + } + }); + } + + else { + mSendHandler.post(new Runnable() { + + @Override + public void run() { + mClient.send("0::" + endpoint); + } + }); + } + } + + /** + * Get the current connected endpoint + * + * @return The current connected endpoint, "socket.io" if connected to the + * default endpoint + */ + public String getConnectedEndpoint() { + return mEndpoint; + } +} From b7e9b765c361f0b4a9a2cfab004576eb39b53930 Mon Sep 17 00:00:00 2001 From: Vinay S Shenoy Date: Mon, 20 May 2013 22:10:39 +0530 Subject: [PATCH 2/2] Added listener event for connecting to endpoints --- src/com/codebutler/android_websockets/SocketIOClient.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/com/codebutler/android_websockets/SocketIOClient.java b/src/com/codebutler/android_websockets/SocketIOClient.java index 58e2268..00a08c2 100644 --- a/src/com/codebutler/android_websockets/SocketIOClient.java +++ b/src/com/codebutler/android_websockets/SocketIOClient.java @@ -25,6 +25,8 @@ public class SocketIOClient { public static interface Handler { public void onConnect(); + public void onConnectToEndpoint(String endpoint); + public void on(String event, JSONArray arguments); public void onDisconnect(int code, String reason); @@ -142,7 +144,11 @@ public void onMessage(String message) { switch (code) { case 1: // connect - mHandler.onConnect(); + if (!TextUtils.isEmpty(parts[2])) { + mHandler.onConnectToEndpoint(parts[2]); + } else { + mHandler.onConnect(); + } break; case 2: // heartbeat