1818
1919import android .net .http .AndroidHttpClient ;
2020import android .os .Looper ;
21+ import android .util .Log ;
2122
2223public class SocketIOClient {
2324 public static interface Handler {
@@ -30,6 +31,8 @@ public static interface Handler {
3031 public void onError (Exception error );
3132 }
3233
34+ private static final String TAG = "SocketIOClient" ;
35+
3336 String mURL ;
3437 Handler mHandler ;
3538 String mSession ;
@@ -76,7 +79,7 @@ public void emit(String name, JSONArray args) throws JSONException {
7679 final JSONObject event = new JSONObject ();
7780 event .put ("name" , name );
7881 event .put ("args" , args );
79- System . out . println ( event .toString ());
82+ Log . d ( TAG , "Emitting event: " + event .toString ());
8083 mSendHandler .post (new Runnable () {
8184 @ Override
8285 public void run () {
@@ -86,7 +89,7 @@ public void run() {
8689 }
8790
8891 private void connectSession () throws URISyntaxException {
89- mClient = new WebSocketClient (new URI (mURL + "websocket/" + mSession ), new WebSocketClient .Handler () {
92+ mClient = new WebSocketClient (new URI (mURL + "websocket/" + mSession ), new WebSocketClient .Listener () {
9093 @ Override
9194 public void onMessage (byte [] data ) {
9295 cleanup ();
@@ -96,7 +99,7 @@ public void onMessage(byte[] data) {
9699 @ Override
97100 public void onMessage (String message ) {
98101 try {
99- System . out . println ( message );
102+ Log . d ( TAG , "Message: " + message );
100103 String [] parts = message .split (":" , 4 );
101104 int code = Integer .parseInt (parts [0 ]);
102105 switch (code ) {
@@ -105,6 +108,7 @@ public void onMessage(String message) {
105108 break ;
106109 case 2 :
107110 // heartbeat
111+ mClient .send ("2::" );
108112 break ;
109113 case 3 :
110114 // message
@@ -185,12 +189,9 @@ public void disconnect() throws IOException {
185189 }
186190
187191 private void cleanup () {
188- try {
189- mClient .disconnect ();
190- mClient = null ;
191- }
192- catch (IOException e ) {
193- }
192+ mClient .disconnect ();
193+ mClient = null ;
194+
194195 mSendLooper .quit ();
195196 mSendLooper = null ;
196197 mSendHandler = null ;
0 commit comments