Skip to content

Commit e1877d6

Browse files
author
Vinay S Shenoy
committed
Added support for Endpoints
1 parent ecaf18b commit e1877d6

File tree

2 files changed

+93
-25
lines changed

2 files changed

+93
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ gen
33
.classpath
44
.project
55
local.properties
6+
/.settings

src/com/codebutler/android_websockets/SocketIOClient.java

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.net.http.AndroidHttpClient;
2020
import android.os.Looper;
21+
import android.text.TextUtils;
2122
import android.util.Log;
2223

2324
public class SocketIOClient {
@@ -36,16 +37,28 @@ public static interface Handler {
3637
}
3738

3839
private static final String TAG = "SocketIOClient";
39-
40+
4041
String mURL;
4142
Handler mHandler;
4243
String mSession;
4344
int mHeartbeat;
4445
WebSocketClient mClient;
46+
String mEndpoint;
4547

4648
public SocketIOClient(URI uri, Handler handler) {
47-
// remove trailing "/" from URI, in case user provided e.g. http://test.com/
48-
mURL = uri.toString().replaceAll("/$", "") + "/socket.io/1/";
49+
this(uri, handler, null);
50+
}
51+
52+
public SocketIOClient(URI uri, Handler handler, String namespace) {
53+
mEndpoint = namespace;
54+
55+
if (TextUtils.isEmpty(namespace)) {
56+
mEndpoint = "socket.io";
57+
}
58+
59+
// remove trailing "/" from URI, in case user provided e.g.
60+
// http://test.com/
61+
mURL = uri.toString().replaceAll("/$", "") + "/" + mEndpoint + "/1/";
4962
mHandler = handler;
5063
}
5164

@@ -54,8 +67,7 @@ private static String downloadUriAsString(final HttpUriRequest req) throws IOExc
5467
try {
5568
HttpResponse res = client.execute(req);
5669
return readToEnd(res.getEntity().getContent());
57-
}
58-
finally {
70+
} finally {
5971
client.close();
6072
}
6173
}
@@ -91,21 +103,21 @@ public void run() {
91103
}
92104
});
93105
}
94-
106+
95107
public void emit(final String message) {
96108
mSendHandler.post(new Runnable() {
97-
109+
98110
@Override
99111
public void run() {
100112
mClient.send(String.format("3:::%s", message));
101113
}
102114
});
103115
}
104-
116+
105117
public void emit(final JSONObject jsonMessage) {
106-
118+
107119
mSendHandler.post(new Runnable() {
108-
120+
109121
@Override
110122
public void run() {
111123
mClient.send(String.format("4:::%s", jsonMessage.toString()));
@@ -140,10 +152,10 @@ public void onMessage(String message) {
140152
// message
141153
final String messageId = parts[1];
142154
final String dataString = parts[3];
143-
144-
if(!"".equals(messageId)) {
155+
156+
if (!"".equals(messageId)) {
145157
mSendHandler.post(new Runnable() {
146-
158+
147159
@Override
148160
public void run() {
149161
mClient.send(String.format("6:::%s", messageId));
@@ -154,20 +166,20 @@ public void run() {
154166
break;
155167
}
156168
case 4: {
157-
//json message
169+
// json message
158170
final String messageId = parts[1];
159171
final String dataString = parts[3];
160-
172+
161173
JSONObject jsonMessage = null;
162-
174+
163175
try {
164176
jsonMessage = new JSONObject(dataString);
165-
} catch(JSONException e) {
177+
} catch (JSONException e) {
166178
jsonMessage = new JSONObject();
167179
}
168-
if(!"".equals(messageId)) {
180+
if (!"".equals(messageId)) {
169181
mSendHandler.post(new Runnable() {
170-
182+
171183
@Override
172184
public void run() {
173185
mClient.send(String.format("6:::%s", messageId));
@@ -211,8 +223,7 @@ public void run() {
211223
default:
212224
throw new Exception("unknown code");
213225
}
214-
}
215-
catch (Exception ex) {
226+
} catch (Exception ex) {
216227
cleanup();
217228
onError(ex);
218229
}
@@ -252,7 +263,7 @@ public void disconnect() throws IOException {
252263
private void cleanup() {
253264
mClient.disconnect();
254265
mClient = null;
255-
266+
256267
mSendLooper.quit();
257268
mSendLooper = null;
258269
mSendHandler = null;
@@ -284,12 +295,68 @@ public void run() {
284295
connectSession();
285296

286297
Looper.loop();
287-
}
288-
catch (Exception e) {
298+
} catch (Exception e) {
289299
mHandler.onError(e);
290300
}
291301
};
292302
}.start();
293303
}
294-
}
295304

305+
/**
306+
* Connect to an endpoint
307+
*/
308+
public void connectToEndpoint(final String endpoint) {
309+
310+
if (mClient.isConnected() && !TextUtils.isEmpty(endpoint)) {
311+
mEndpoint = endpoint;
312+
mSendHandler.post(new Runnable() {
313+
314+
@Override
315+
public void run() {
316+
mClient.send("1::" + endpoint);
317+
}
318+
});
319+
}
320+
}
321+
322+
/**
323+
* Disconnect from an endpoint or socket
324+
*
325+
* @param endpoint
326+
* {@code null} to disconnect the entire socket, otherwise the
327+
* endpoint to disconnect from
328+
*/
329+
public void sendDisconnect(final String endpoint) {
330+
331+
if (TextUtils.isEmpty(endpoint)) {
332+
333+
mSendHandler.post(new Runnable() {
334+
335+
@Override
336+
public void run() {
337+
mClient.send("0");
338+
}
339+
});
340+
}
341+
342+
else {
343+
mSendHandler.post(new Runnable() {
344+
345+
@Override
346+
public void run() {
347+
mClient.send("0::" + endpoint);
348+
}
349+
});
350+
}
351+
}
352+
353+
/**
354+
* Get the current connected endpoint
355+
*
356+
* @return The current connected endpoint, "socket.io" if connected to the
357+
* default endpoint
358+
*/
359+
public String getConnectedEndpoint() {
360+
return mEndpoint;
361+
}
362+
}

0 commit comments

Comments
 (0)