1818
1919import android .net .http .AndroidHttpClient ;
2020import android .os .Looper ;
21+ import android .text .TextUtils ;
2122import android .util .Log ;
2223
2324public class SocketIOClient {
2425 public static interface Handler {
2526 public void onConnect ();
2627
28+ public void onConnectToEndpoint (String endpoint );
29+
2730 public void on (String event , JSONArray arguments );
2831
2932 public void onDisconnect (int code , String reason );
@@ -36,16 +39,28 @@ public static interface Handler {
3639 }
3740
3841 private static final String TAG = "SocketIOClient" ;
39-
42+
4043 String mURL ;
4144 Handler mHandler ;
4245 String mSession ;
4346 int mHeartbeat ;
4447 WebSocketClient mClient ;
48+ String mEndpoint ;
4549
4650 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/" ;
51+ this (uri , handler , null );
52+ }
53+
54+ public SocketIOClient (URI uri , Handler handler , String namespace ) {
55+ mEndpoint = namespace ;
56+
57+ if (TextUtils .isEmpty (namespace )) {
58+ mEndpoint = "socket.io" ;
59+ }
60+
61+ // remove trailing "/" from URI, in case user provided e.g.
62+ // http://test.com/
63+ mURL = uri .toString ().replaceAll ("/$" , "" ) + "/" + mEndpoint + "/1/" ;
4964 mHandler = handler ;
5065 }
5166
@@ -54,8 +69,7 @@ private static String downloadUriAsString(final HttpUriRequest req) throws IOExc
5469 try {
5570 HttpResponse res = client .execute (req );
5671 return readToEnd (res .getEntity ().getContent ());
57- }
58- finally {
72+ } finally {
5973 client .close ();
6074 }
6175 }
@@ -91,21 +105,21 @@ public void run() {
91105 }
92106 });
93107 }
94-
108+
95109 public void emit (final String message ) {
96110 mSendHandler .post (new Runnable () {
97-
111+
98112 @ Override
99113 public void run () {
100114 mClient .send (String .format ("3:::%s" , message ));
101115 }
102116 });
103117 }
104-
118+
105119 public void emit (final JSONObject jsonMessage ) {
106-
120+
107121 mSendHandler .post (new Runnable () {
108-
122+
109123 @ Override
110124 public void run () {
111125 mClient .send (String .format ("4:::%s" , jsonMessage .toString ()));
@@ -130,7 +144,11 @@ public void onMessage(String message) {
130144 switch (code ) {
131145 case 1 :
132146 // connect
133- mHandler .onConnect ();
147+ if (!TextUtils .isEmpty (parts [2 ])) {
148+ mHandler .onConnectToEndpoint (parts [2 ]);
149+ } else {
150+ mHandler .onConnect ();
151+ }
134152 break ;
135153 case 2 :
136154 // heartbeat
@@ -140,10 +158,10 @@ public void onMessage(String message) {
140158 // message
141159 final String messageId = parts [1 ];
142160 final String dataString = parts [3 ];
143-
144- if (!"" .equals (messageId )) {
161+
162+ if (!"" .equals (messageId )) {
145163 mSendHandler .post (new Runnable () {
146-
164+
147165 @ Override
148166 public void run () {
149167 mClient .send (String .format ("6:::%s" , messageId ));
@@ -154,20 +172,20 @@ public void run() {
154172 break ;
155173 }
156174 case 4 : {
157- //json message
175+ // json message
158176 final String messageId = parts [1 ];
159177 final String dataString = parts [3 ];
160-
178+
161179 JSONObject jsonMessage = null ;
162-
180+
163181 try {
164182 jsonMessage = new JSONObject (dataString );
165- } catch (JSONException e ) {
183+ } catch (JSONException e ) {
166184 jsonMessage = new JSONObject ();
167185 }
168- if (!"" .equals (messageId )) {
186+ if (!"" .equals (messageId )) {
169187 mSendHandler .post (new Runnable () {
170-
188+
171189 @ Override
172190 public void run () {
173191 mClient .send (String .format ("6:::%s" , messageId ));
@@ -211,8 +229,7 @@ public void run() {
211229 default :
212230 throw new Exception ("unknown code" );
213231 }
214- }
215- catch (Exception ex ) {
232+ } catch (Exception ex ) {
216233 cleanup ();
217234 onError (ex );
218235 }
@@ -252,7 +269,7 @@ public void disconnect() throws IOException {
252269 private void cleanup () {
253270 mClient .disconnect ();
254271 mClient = null ;
255-
272+
256273 mSendLooper .quit ();
257274 mSendLooper = null ;
258275 mSendHandler = null ;
@@ -284,12 +301,68 @@ public void run() {
284301 connectSession ();
285302
286303 Looper .loop ();
287- }
288- catch (Exception e ) {
304+ } catch (Exception e ) {
289305 mHandler .onError (e );
290306 }
291307 };
292308 }.start ();
293309 }
294- }
295310
311+ /**
312+ * Connect to an endpoint
313+ */
314+ public void connectToEndpoint (final String endpoint ) {
315+
316+ if (mClient .isConnected () && !TextUtils .isEmpty (endpoint )) {
317+ mEndpoint = endpoint ;
318+ mSendHandler .post (new Runnable () {
319+
320+ @ Override
321+ public void run () {
322+ mClient .send ("1::" + endpoint );
323+ }
324+ });
325+ }
326+ }
327+
328+ /**
329+ * Disconnect from an endpoint or socket
330+ *
331+ * @param endpoint
332+ * {@code null} to disconnect the entire socket, otherwise the
333+ * endpoint to disconnect from
334+ */
335+ public void sendDisconnect (final String endpoint ) {
336+
337+ if (TextUtils .isEmpty (endpoint )) {
338+
339+ mSendHandler .post (new Runnable () {
340+
341+ @ Override
342+ public void run () {
343+ mClient .send ("0" );
344+ }
345+ });
346+ }
347+
348+ else {
349+ mSendHandler .post (new Runnable () {
350+
351+ @ Override
352+ public void run () {
353+ mClient .send ("0::" + endpoint );
354+ }
355+ });
356+ }
357+ }
358+
359+ /**
360+ * Get the current connected endpoint
361+ *
362+ * @return The current connected endpoint, "socket.io" if connected to the
363+ * default endpoint
364+ */
365+ public String getConnectedEndpoint () {
366+ return mEndpoint ;
367+ }
368+ }
0 commit comments