Skip to content

Commit 518eebe

Browse files
authored
Merge pull request #146 from Jordan-Turgeon/multiple-topic-subscriptions
#142 Fixed multiple topic subscription errors
2 parents b59b1e7 + a8cf433 commit 518eebe

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,12 @@ public Flowable<StompMessage> topic(@NonNull String destPath, List<StompHeader>
242242
return Flowable.error(new IllegalArgumentException("Topic path cannot be null"));
243243
else if (!streamMap.containsKey(destPath))
244244
streamMap.put(destPath,
245-
subscribePath(destPath, headerList).andThen(
245+
Completable.defer(() -> subscribePath(destPath, headerList)).andThen(
246246
getMessageStream()
247247
.filter(msg -> pathMatcher.matches(destPath, msg))
248248
.toFlowable(BackpressureStrategy.BUFFER)
249-
.share()).doFinally(() -> unsubscribePath(destPath).subscribe())
249+
.doFinally(() -> unsubscribePath(destPath).subscribe())
250+
.share())
250251
);
251252
return streamMap.get(destPath);
252253
}
@@ -269,14 +270,20 @@ private Completable subscribePath(String destinationPath, @Nullable List<StompHe
269270
headers.add(new StompHeader(StompHeader.ACK, DEFAULT_ACK));
270271
if (headerList != null) headers.addAll(headerList);
271272
return send(new StompMessage(StompCommand.SUBSCRIBE,
272-
headers, null));
273+
headers, null))
274+
.doOnError(throwable -> unsubscribePath(destinationPath).subscribe());
273275
}
274276

275277

276278
private Completable unsubscribePath(String dest) {
277279
streamMap.remove(dest);
278280

279281
String topicId = topics.get(dest);
282+
283+
if (topicId == null) {
284+
return Completable.complete();
285+
}
286+
280287
topics.remove(dest);
281288

282289
Log.d(TAG, "Unsubscribe path: " + dest + " id: " + topicId);

0 commit comments

Comments
 (0)