Skip to content

Commit e33c05b

Browse files
committed
Added heartbeat replies, fixed two bugs, and added logging.
Client will now send a heartbeat back to the server when it receives one, so the connection won't time out.  Removed unreachable catch block in cleanup() method, and changed WebSocketClient.Handler() to a Listener() to match the recent change to to the WebSocketClient interface. Changed print statements to use Log, to be consistent with the rest of the package.
1 parent 18a3624 commit e33c05b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/com/codebutler/android_websockets/SocketIOClient.java

Lines changed: 10 additions & 9 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.util.Log;
2122

2223
public 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

Comments
 (0)