Skip to content

Commit 608d748

Browse files
committed
Try to fix ssl memory leak
1 parent 603b24a commit 608d748

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0-rc1'
8+
classpath 'com.android.tools.build:gradle:2.3.0'
99
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
1010
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1111

example-client/src/main/java/ua/naiksoftware/stompclientexample/MainActivity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ protected void onCreate(Bundle savedInstanceState) {
4949
mAdapter.setHasStableIds(true);
5050
mRecyclerView.setAdapter(mAdapter);
5151
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true));
52+
}
5253

53-
connectStomp();
54+
public void disconnectStomp(View view) {
55+
mStompClient.disconnect();
5456
}
5557

56-
private void connectStomp() {
58+
public void connectStomp(View view) {
5759
mStompClient = Stomp.over(WebSocket.class, "ws://" + ANDROID_EMULATOR_LOCALHOST
5860
+ ":" + RestClient.SERVER_PORT + "/example-endpoint/websocket");
5961

example-client/src/main/res/layout/activity_main.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77
android:orientation="vertical"
88
tools:context="ua.naiksoftware.stompclientexample.MainActivity">
99

10+
<LinearLayout
11+
style="?android:buttonBarStyle"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:orientation="horizontal">
15+
16+
<Button
17+
style="?android:buttonBarButtonStyle"
18+
android:layout_width="0dp"
19+
android:layout_height="wrap_content"
20+
android:layout_weight="1"
21+
android:text="@string/connect_stomp"
22+
android:onClick="connectStomp"/>
23+
24+
<Button
25+
style="?android:buttonBarButtonStyle"
26+
android:layout_width="0dp"
27+
android:layout_height="wrap_content"
28+
android:layout_weight="1"
29+
android:text="@string/disconnect_stomp"
30+
android:onClick="disconnectStomp"/>
31+
32+
</LinearLayout>
33+
34+
<View
35+
android:layout_width="match_parent"
36+
android:layout_height="1dp"
37+
android:background="#eee"
38+
android:layout_marginLeft="16dp"
39+
android:layout_marginRight="16dp"/>
40+
1041
<LinearLayout
1142
style="?android:buttonBarStyle"
1243
android:layout_width="match_parent"

example-client/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
<string name="app_name">Example client</string>
33
<string name="send_mode_1">Echo STOMP</string>
44
<string name="send_mode_2">Echo REST</string>
5+
<string name="connect_stomp">Connect STOMP</string>
6+
<string name="disconnect_stomp">DISCONNECT STOMP</string>
57
</resources>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ua.naiksoftware.stomp;
22

3+
import android.os.Looper;
34
import android.util.Log;
45

56
import org.java_websocket.WebSocket;
@@ -60,7 +61,10 @@ public Observable<String> messages() {
6061
if (iterator.next().isUnsubscribed()) iterator.remove();
6162
}
6263

63-
if (mMessagesSubscribers.size() < 1) mWebSocketClient.close();
64+
if (mMessagesSubscribers.size() < 1) {
65+
Log.d(TAG, "Close web socket connection now in thread " + Thread.currentThread() + " in main loop? " + (Looper.getMainLooper() == Looper.myLooper()));
66+
mWebSocketClient.close();
67+
}
6468
});
6569

6670
createWebSocketConnection();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import rx.Subscriber;
1616
import rx.Subscription;
1717
import rx.observables.ConnectableObservable;
18+
import rx.schedulers.Schedulers;
1819
import ua.naiksoftware.stomp.ConnectionProvider;
1920
import ua.naiksoftware.stomp.LifecycleEvent;
2021
import ua.naiksoftware.stomp.StompHeader;
@@ -95,6 +96,7 @@ public void connect(List<StompHeader> _headers, boolean reconnect) {
9596

9697
isConnecting = true;
9798
mMessagesSubscription = mConnectionProvider.messages()
99+
.unsubscribeOn(Schedulers.io())
98100
.map(StompMessage::from)
99101
.subscribe(stompMessage -> {
100102
if (stompMessage.getStompCommand().equals(StompCommand.CONNECTED)) {

0 commit comments

Comments
 (0)