Skip to content

Commit

Permalink
Merge pull request codebutler#16 from vinaysshenoy/master
Browse files Browse the repository at this point in the history
Added Support for Endpoints
  • Loading branch information
koush committed May 20, 2013
2 parents ecaf18b + b7e9b76 commit 96ae660
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ gen
.classpath
.project
local.properties
/.settings
125 changes: 99 additions & 26 deletions src/com/codebutler/android_websockets/SocketIOClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import android.net.http.AndroidHttpClient;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

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);
Expand All @@ -36,16 +39,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;
}

Expand All @@ -54,8 +69,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();
}
}
Expand Down Expand Up @@ -91,21 +105,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()));
Expand All @@ -130,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
Expand All @@ -140,10 +158,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));
Expand All @@ -154,20 +172,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));
Expand Down Expand Up @@ -211,8 +229,7 @@ public void run() {
default:
throw new Exception("unknown code");
}
}
catch (Exception ex) {
} catch (Exception ex) {
cleanup();
onError(ex);
}
Expand Down Expand Up @@ -252,7 +269,7 @@ public void disconnect() throws IOException {
private void cleanup() {
mClient.disconnect();
mClient = null;

mSendLooper.quit();
mSendLooper = null;
mSendHandler = null;
Expand Down Expand Up @@ -284,12 +301,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;
}
}

0 comments on commit 96ae660

Please sign in to comment.