Skip to content

Commit d272400

Browse files
author
Nickolay Savchenko
committed
Merge akuhtz fixes
1 parent cff4f89 commit d272400

File tree

15 files changed

+131
-67
lines changed

15 files changed

+131
-67
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
.idea
55
.DS_Store
66
/build
7-
/captures
7+
/captures
8+
test-server/build

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-client/build.gradle

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
buildscript {
2-
repositories {
3-
jcenter()
4-
maven {
5-
url "https://maven.google.com"
6-
}
7-
google()
8-
}
9-
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.2.1'
11-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
12-
}
13-
}
14-
15-
repositories {
16-
jcenter()
17-
maven { url "https://jitpack.io" }
18-
google()
19-
}
201

212
apply plugin: 'com.android.application'
223

234
android {
245
compileSdkVersion 28
25-
buildToolsVersion '28.0.3'
6+
//buildToolsVersion '28.0.3'
267

278
defaultConfig {
289
applicationId "ua.naiksoftware.stompclientexample"
@@ -48,7 +29,12 @@ dependencies {
4829
implementation 'com.android.support:appcompat-v7:28.0.0'
4930
implementation 'org.java-websocket:Java-WebSocket:1.3.6'
5031
implementation 'com.android.support:recyclerview-v7:28.0.0'
51-
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
32+
33+
// RxJava
34+
// implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
35+
implementation 'io.reactivex.rxjava2:rxjava:2.1.15'
36+
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
37+
5238
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
5339
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
5440
implementation 'com.squareup.retrofit2:retrofit:2.3.0'

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import io.reactivex.CompletableTransformer;
2121
import io.reactivex.android.schedulers.AndroidSchedulers;
22+
import io.reactivex.disposables.CompositeDisposable;
2223
import io.reactivex.disposables.Disposable;
2324
import io.reactivex.schedulers.Schedulers;
2425
import ua.naiksoftware.stomp.Stomp;
26+
import ua.naiksoftware.stomp.StompHeader;
2527
import ua.naiksoftware.stomp.client.StompClient;
2628

2729
import static ua.naiksoftware.stompclientexample.RestClient.ANDROID_EMULATOR_LOCALHOST;
@@ -38,6 +40,8 @@ public class MainActivity extends AppCompatActivity {
3840
private RecyclerView mRecyclerView;
3941
private Gson mGson = new GsonBuilder().create();
4042

43+
private CompositeDisposable compositeDisposable;
44+
4145
@Override
4246
protected void onCreate(Bundle savedInstanceState) {
4347
super.onCreate(savedInstanceState);
@@ -51,13 +55,32 @@ protected void onCreate(Bundle savedInstanceState) {
5155

5256
public void disconnectStomp(View view) {
5357
mStompClient.disconnect();
58+
59+
if (compositeDisposable != null) {
60+
compositeDisposable.dispose();
61+
62+
compositeDisposable = null;
63+
}
5464
}
5565

66+
public static final String LOGIN = "login";
67+
68+
public static final String PASSCODE = "passcode";
69+
5670
public void connectStomp(View view) {
71+
72+
List<StompHeader> headers = new ArrayList<>();
73+
headers.add(new StompHeader(LOGIN, "guest"));
74+
headers.add(new StompHeader(PASSCODE, "guest"));
75+
5776
mStompClient = Stomp.over(Stomp.ConnectionProvider.JWS, "ws://" + ANDROID_EMULATOR_LOCALHOST
5877
+ ":" + RestClient.SERVER_PORT + "/example-endpoint/websocket");
5978

60-
mStompClient.lifecycle()
79+
mStompClient.withClientHeartbeat(30000).withServerHeartbeat(30000);
80+
81+
compositeDisposable = new CompositeDisposable();
82+
83+
Disposable dispLifecycle = mStompClient.lifecycle()
6184
.subscribeOn(Schedulers.io())
6285
.observeOn(AndroidSchedulers.mainThread())
6386
.subscribe(lifecycleEvent -> {
@@ -71,19 +94,27 @@ public void connectStomp(View view) {
7194
break;
7295
case CLOSED:
7396
toast("Stomp connection closed");
97+
break;
98+
case FAILED_SERVER_HEARTBEAT:
99+
toast("Stomp failed server heartbeat");
100+
break;
74101
}
75102
});
76103

104+
compositeDisposable.add(dispLifecycle);
105+
77106
// Receive greetings
78-
mStompClient.topic("/topic/greetings")
107+
Disposable dispTopic = mStompClient.topic("/topic/greetings")
79108
.subscribeOn(Schedulers.io())
80109
.observeOn(AndroidSchedulers.mainThread())
81110
.subscribe(topicMessage -> {
82111
Log.d(TAG, "Received " + topicMessage.getPayload());
83112
addItem(mGson.fromJson(topicMessage.getPayload(), EchoModel.class));
84113
});
85114

86-
mStompClient.connect();
115+
compositeDisposable.add(dispTopic);
116+
117+
mStompClient.connect(headers);
87118
}
88119

89120
public void sendEchoViaStomp(View v) {
@@ -130,6 +161,12 @@ protected CompletableTransformer applySchedulers() {
130161
@Override
131162
protected void onDestroy() {
132163
mStompClient.disconnect();
164+
165+
if (compositeDisposable != null) {
166+
compositeDisposable.dispose();
167+
compositeDisposable = null;
168+
}
169+
133170
if (mRestPingDisposable != null) mRestPingDisposable.dispose();
134171
super.onDestroy();
135172
}

lib/build.gradle

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
buildscript {
2-
repositories {
3-
jcenter()
4-
maven {
5-
url "https://maven.google.com"
6-
}
7-
google()
8-
}
9-
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.2.1'
11-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
12-
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:2.0.0'
13-
}
14-
}
15-
16-
repositories {
17-
jcenter()
18-
google()
19-
}
201

212
apply plugin: 'com.android.library'
223
apply plugin: 'com.github.dcendents.android-maven'
@@ -25,7 +6,7 @@ apply plugin: 'groovyx.android'
256
group='com.github.NaikSoftware'
267

278
android {
28-
compileSdkVersion 28
9+
compileSdkVersion 27
2910

3011
defaultConfig {
3112
minSdkVersion 16
@@ -54,6 +35,7 @@ android {
5435

5536
dependencies {
5637
implementation "io.reactivex.rxjava2:rxjava:2.1.15"
38+
5739
// Supported transports
5840
api 'org.java-websocket:Java-WebSocket:1.3.6'
5941
api 'com.squareup.okhttp3:okhttp:3.11.0'

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,16 @@ public Completable disconnect() {
6666
if (clientSendHeartBeatTask != null) {
6767
clientSendHeartBeatTask.dispose();
6868
}
69-
scheduler.shutdown();
70-
Log.d(TAG, "Shutting down heart-beat scheduler...");
69+
70+
if (serverCheckHeartBeatTask != null) {
71+
serverCheckHeartBeatTask.dispose();
72+
}
73+
74+
lastServerHeartBeat = 0;
75+
76+
// TODO shutdown Schedulers.io() is not a good idea
77+
// scheduler.shutdown();
78+
7179
return Completable
7280
.fromAction(this::rawDisconnect);
7381
}
@@ -130,7 +138,9 @@ void emitLifecycleEvent(@NonNull LifecycleEvent lifecycleEvent) {
130138
void emitMessage(String stompMessage) {
131139
//TODO: Why we don't publish a StompMessage, instead of String? will this connection provider work with other protocol?
132140
final StompMessage sm = StompMessage.from(stompMessage);
141+
133142
if (StompCommand.CONNECTED.equals(sm.getStompCommand())) {
143+
Log.d(TAG, "<<< CONNECTED");
134144
heartBeatHandshake(sm.findHeader(StompHeader.HEART_BEAT));
135145
} else if (StompCommand.SEND.equals(sm.getStompCommand())) {
136146
abortClientHeartBeatSend();
@@ -175,7 +185,6 @@ private void heartBeatHandshake(final String heartBeatHeader) {
175185
}
176186
if (clientHeartbeat > 0 || serverHeartbeat > 0) {
177187
scheduler = Schedulers.io();
178-
179188
if (clientHeartbeat > 0) {
180189
//client MUST/WANT send heart-beat
181190
Log.d(TAG, "Client will send heart-beat every " + clientHeartbeat + " ms");
@@ -185,13 +194,17 @@ private void heartBeatHandshake(final String heartBeatHeader) {
185194
Log.d(TAG, "Client will listen to server heart-beat every " + serverHeartbeat + " ms");
186195
//client WANT to listen to server heart-beat
187196
scheduleServerHeartBeatCheck();
197+
198+
// initialize the server heartbeat
199+
lastServerHeartBeat = System.currentTimeMillis();
188200
}
189201
}
190202
}
191203

192204
protected void scheduleServerHeartBeatCheck() {
193205
if (serverHeartbeat > 0 && scheduler != null) {
194-
Log.d(TAG, "Scheduling server heart-beat to be checked in " + serverHeartbeat + " ms");
206+
final long now = System.currentTimeMillis();
207+
Log.d(TAG, "Scheduling server heart-beat to be checked in " + serverHeartbeat + " ms and now is '" + now + "'");
195208
//add some slack on the check
196209
serverCheckHeartBeatTask = scheduler.scheduleDirect(() ->
197210
checkServerHeartBeat(), serverHeartbeat, TimeUnit.MILLISECONDS);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ public interface ConnectionProvider {
4545
* @param ms milliseconds
4646
*/
4747
void setClientHeartbeat(int ms);
48-
4948
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class LifecycleEvent {
1010

1111
public enum Type {
12-
OPENED, CLOSED, ERROR, FAILED_SERVER_HEARTBEAT;
12+
OPENED, CLOSED, ERROR, FAILED_SERVER_HEARTBEAT
1313
}
1414

1515
private final Type mType;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
public class StompHeader {
77

8-
public static final String VERSION = "version";
8+
public static final String VERSION = "accept-version";
99
public static final String HEART_BEAT = "heart-beat";
1010
public static final String DESTINATION = "destination";
1111
public static final String CONTENT_TYPE = "content-type";

0 commit comments

Comments
 (0)