Skip to content

Commit d796357

Browse files
Working on reconnection
1 parent 14e9dd6 commit d796357

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
11+
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
1212
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1313

1414
// NOTE: Do not place your application dependencies here; they belong

lib/src/main/java/ua/naiksoftware/stomp/OkHttpConnectionProvider.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
5+
import android.util.Log;
56

67
import java.util.HashMap;
78
import java.util.Map;
@@ -22,6 +23,7 @@ class OkHttpConnectionProvider extends AbstractConnectionProvider {
2223
@NonNull
2324
private final Map<String, String> mConnectHttpHeaders;
2425
private final OkHttpClient mOkHttpClient;
26+
private final String tag = OkHttpConnectionProvider.class.getSimpleName();
2527

2628
@Nullable
2729
private WebSocket openedSocked;
@@ -36,7 +38,11 @@ class OkHttpConnectionProvider extends AbstractConnectionProvider {
3638
@NonNull
3739
@Override
3840
public Completable disconnect() {
39-
return Completable.fromAction(() -> openedSocked.close(1000, ""));
41+
if (openedSocked == null) {
42+
return Completable.error(new IllegalStateException("Attempted to disconnect when already disconnected"));
43+
}
44+
return Completable
45+
.fromAction(() -> openedSocked.close(1000, ""));
4046
}
4147

4248
@Override
@@ -65,7 +71,10 @@ public void onOpen(WebSocket webSocket, @NonNull Response response) {
6571

6672
@Override
6773
public void onMessage(WebSocket webSocket, String text) {
68-
emitMessage(text);
74+
if (text.equals("\n"))
75+
Log.d(tag, "RECEIVED HEARTBEAT");
76+
else
77+
emitMessage(text);
6978
}
7079

7180
@Override

lib/src/main/java/ua/naiksoftware/stomp/client/StompClient.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class StompClient {
2929
public static final String SUPPORTED_VERSIONS = "1.1,1.0";
3030
public static final String DEFAULT_ACK = "auto";
3131

32+
private final String tag = StompClient.class.getSimpleName();
3233
private final ConnectionProvider mConnectionProvider;
3334
private HashMap<String, String> mTopics;
3435
private boolean mConnected;
@@ -42,9 +43,13 @@ public class StompClient {
4243
public StompClient(ConnectionProvider connectionProvider) {
4344
mConnectionProvider = connectionProvider;
4445
mMessageStream = PublishSubject.create();
46+
mStreamMap = new HashMap<>();
47+
resetStatus();
48+
}
49+
50+
private void resetStatus() {
4551
mConnectionFuture = new CompletableFuture<>();
4652
mConnectionComplete = Completable.fromFuture(mConnectionFuture).subscribeOn(Schedulers.newThread());
47-
mStreamMap = new HashMap<>();
4853
}
4954

5055
/**
@@ -61,7 +66,7 @@ public void connect(boolean reconnect) {
6166
/**
6267
* Connect without reconnect if connected
6368
*
64-
* @param _headers might be null
69+
* @param _headers HTTP headers to send in the INITIAL REQUEST, i.e. during the protocol upgrade
6570
*/
6671
public void connect(List<StompHeader> _headers) {
6772
connect(_headers, false);
@@ -70,7 +75,7 @@ public void connect(List<StompHeader> _headers) {
7075
/**
7176
* If already connected and reconnect=false - nope
7277
*
73-
* @param _headers might be null
78+
* @param _headers HTTP headers to send in the INITIAL REQUEST, i.e. during the protocol upgrade
7479
*/
7580
public void connect(@Nullable List<StompHeader> _headers, boolean reconnect) {
7681
if (reconnect) disconnect();
@@ -123,7 +128,6 @@ public Completable send(String destination, String data) {
123128

124129
public Completable send(@NonNull StompMessage stompMessage) {
125130
Completable completable = mConnectionProvider.send(stompMessage.compile());
126-
mConnectionComplete.subscribe();
127131
return completable.startWith(mConnectionComplete);
128132
}
129133

@@ -136,7 +140,8 @@ public Observable<LifecycleEvent> lifecycle() {
136140
}
137141

138142
public void disconnect() {
139-
mConnectionProvider.disconnect().subscribe();
143+
resetStatus();
144+
mConnectionProvider.disconnect().subscribe(() -> mConnected = false);
140145
}
141146

142147
public Observable<StompMessage> topic(String destinationPath) {

0 commit comments

Comments
 (0)