Skip to content

Commit cb10e8b

Browse files
Fixed protocol breach with message format; removed Clojars dependency.
Also cleaned up some remnants of the merge in the example client.
1 parent 24da370 commit cb10e8b

File tree

8 files changed

+36
-11
lines changed

8 files changed

+36
-11
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ You can use this library two ways:
3232
- Using the new Native Java 8 support
3333
- As of this writing, you must be using Android Studio Beta to use this feature.
3434
- Has been tested in the following environments:
35-
- Beta 1-9, Gradle plugin v3.0.0-beta(1-9)
35+
- Beta 1-6, Gradle plugin v3.0.0-beta(1-6)
3636
- Canary 8-9, Gradle plugin v3.0.0-alpha(8-9)
3737
- It *should* work in all 3.0.0+ versions
3838
- You can find more info on the [Releases Page](https://github.com/forresthopkinsa/StompProtocolAndroid/releases)
@@ -267,6 +267,10 @@ These are the possible changes you need to make to your code for this branch, if
267267
- Rudimentary heartbeat mechanism
268268
- You can use `StompClient.setHeartbeat(ms interval)` to send a heartbeat header to the server
269269
- WIP; currently we don't deal with those heartbeats in any way other than printing them to console
270+
- Better adherence to STOMP spec
271+
- According to the [spec](http://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames), the end of the message body should be immediately followed by a NULL octet, marking the end of the frame.
272+
- Before, we were adding an extra two newlines between the body and NULL octet, which was breaking compatibility with picky servers.
273+
- This shouldn't make any difference, but if it does, you can revert to legacy formatting with `client.setLegacyWhitespace(true)`.
270274
271275
## Additional Reading
272276

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ allprojects {
2020
repositories {
2121
jcenter()
2222
maven { url "https://jitpack.io" }
23-
maven { url "http://clojars.org/repo" }
2423
}
2524
}
2625

example-client/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies {
2828
compile fileTree(dir: 'libs', include: ['*.jar'])
2929
testCompile 'junit:junit:4.12'
3030
compile 'com.android.support:appcompat-v7:25.2.0'
31-
compile 'org.java-websocket:java-websocket:1.3.2'
31+
compile 'org.java-websocket:Java-WebSocket:1.3.4'
3232
compile 'com.android.support:recyclerview-v7:25.2.0'
3333
compile 'io.reactivex:rxandroid:1.2.1'
3434
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package ua.naiksoftware.stompclientexample;
22

3-
import io.reactivex.Flowable;
43
import retrofit2.http.POST;
54
import retrofit2.http.Query;
5+
import rx.Observable;
66

77
/**
88
* Created by Naik on 24.02.17.
99
*/
1010
public interface ExampleRepository {
1111

1212
@POST("hello-convert-and-send")
13-
Flowable<Void> sendRestEcho(@Query("msg") String message);
13+
Observable<Void> sendRestEcho(@Query("msg") String message);
1414
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ua.naiksoftware.stompclientexample;
22

33
import retrofit2.Retrofit;
4-
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
4+
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
55
import retrofit2.converter.gson.GsonConverterFactory;
66

77
/**
@@ -33,7 +33,7 @@ public static RestClient getInstance() {
3333
private RestClient() {
3434
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://" + ANDROID_EMULATOR_LOCALHOST + ":" + SERVER_PORT + "/")
3535
.addConverterFactory(GsonConverterFactory.create())
36-
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
36+
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
3737
.build();
3838
mExampleRepository = retrofit.create(ExampleRepository.class);
3939
}

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies {
3535
compile 'net.sourceforge.streamsupport:streamsupport:1.5.5'
3636
compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.5.5'
3737
// Supported transports
38-
compile 'org.java-websocket:java-websocket:1.3.2'
38+
compile 'org.java-websocket:Java-WebSocket:1.3.4'
3939
compile 'com.squareup.okhttp3:okhttp:3.8.1'
4040
implementation 'com.android.support:support-annotations:24.2.0'
4141
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class StompClient {
3636
private ConcurrentHashMap<String, String> mTopics;
3737
private boolean mConnected;
3838
private boolean isConnecting;
39+
private boolean legacyWhitespace;
3940

4041
private PublishSubject<StompMessage> mMessageStream;
4142
private CompletableFuture<Boolean> mConnectionFuture;
@@ -115,7 +116,7 @@ public void connect(@Nullable List<StompHeader> _headers) {
115116
headers.add(new StompHeader(StompHeader.VERSION, SUPPORTED_VERSIONS));
116117
headers.add(new StompHeader(StompHeader.HEART_BEAT, "0," + heartbeat));
117118
if (_headers != null) headers.addAll(_headers);
118-
mConnectionProvider.send(new StompMessage(StompCommand.CONNECT, headers, null).compile())
119+
mConnectionProvider.send(new StompMessage(StompCommand.CONNECT, headers, null).compile(legacyWhitespace))
119120
.subscribe();
120121
break;
121122

@@ -163,7 +164,7 @@ public Completable send(String destination, String data) {
163164
}
164165

165166
public Completable send(@NonNull StompMessage stompMessage) {
166-
Completable completable = mConnectionProvider.send(stompMessage.compile());
167+
Completable completable = mConnectionProvider.send(stompMessage.compile(legacyWhitespace));
167168
return completable.startWith(mConnectionComplete);
168169
}
169170

@@ -199,6 +200,21 @@ else if (!mStreamMap.containsKey(destPath))
199200
return mStreamMap.get(destPath);
200201
}
201202

203+
/**
204+
* Reverts to the old frame formatting, which included two newlines between the message body
205+
* and the end-of-frame marker.
206+
* <p>
207+
* Before: Body\n\n^@
208+
* <p>
209+
* After: Body^@
210+
*
211+
* @param legacyWhitespace whether to append an extra two newlines
212+
* @see <a href="http://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames">The STOMP spec</a>
213+
*/
214+
public void setLegacyWhitespace(boolean legacyWhitespace) {
215+
this.legacyWhitespace = legacyWhitespace;
216+
}
217+
202218
private boolean matches(String path, StompMessage msg) {
203219
String dest = msg.findHeader(StompHeader.DESTINATION);
204220
if (dest == null) return false;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ public String findHeader(String key) {
5454

5555
@NonNull
5656
public String compile() {
57+
return compile(false);
58+
}
59+
60+
@NonNull
61+
public String compile(boolean legacyWhitespace) {
5762
StringBuilder builder = new StringBuilder();
5863
builder.append(mStompCommand).append('\n');
5964
for (StompHeader header : mStompHeaders) {
6065
builder.append(header.getKey()).append(':').append(header.getValue()).append('\n');
6166
}
6267
builder.append('\n');
6368
if (mPayload != null) {
64-
builder.append(mPayload).append("\n\n");
69+
builder.append(mPayload);
70+
if (legacyWhitespace) builder.append("\n\n");
6571
}
6672
builder.append(TERMINATE_MESSAGE_SYMBOL);
6773
return builder.toString();

0 commit comments

Comments
 (0)