Skip to content

Commit ca690b8

Browse files
committed
Add binary‑message support to WebSocketMessage API
- fix #3825
1 parent 2f5c682 commit ca690b8

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

jooby/src/main/java/io/jooby/WebSocketMessage.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.jooby;
77

88
import java.lang.reflect.Type;
9+
import java.nio.ByteBuffer;
910
import java.nio.charset.StandardCharsets;
1011

1112
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -27,7 +28,21 @@ public interface WebSocketMessage extends Value {
2728
* @param <T> Element type.
2829
* @return Instance of the type.
2930
*/
30-
@NonNull <T> T to(@NonNull Type type);
31+
<T> T to(@NonNull Type type);
32+
33+
/**
34+
* Direct access to bytes.
35+
*
36+
* @return Direct access to bytes.
37+
*/
38+
byte[] bytes();
39+
40+
/**
41+
* Direct access to bytes.
42+
*
43+
* @return Direct access to bytes.
44+
*/
45+
ByteBuffer byteBuffer();
3146

3247
/**
3348
* Creates a websocket message.
@@ -36,7 +51,7 @@ public interface WebSocketMessage extends Value {
3651
* @param bytes Text message as byte array.
3752
* @return A websocket message.
3853
*/
39-
static @NonNull WebSocketMessage create(@NonNull Context ctx, @NonNull byte[] bytes) {
54+
static WebSocketMessage create(@NonNull Context ctx, @NonNull byte[] bytes) {
4055
return new WebSocketMessageImpl(ctx, bytes);
4156
}
4257

@@ -47,7 +62,7 @@ public interface WebSocketMessage extends Value {
4762
* @param message Text message.
4863
* @return A websocket message.
4964
*/
50-
static @NonNull WebSocketMessage create(@NonNull Context ctx, @NonNull String message) {
65+
static WebSocketMessage create(@NonNull Context ctx, @NonNull String message) {
5166
return new WebSocketMessageImpl(ctx, message.getBytes(StandardCharsets.UTF_8));
5267
}
5368
}

jooby/src/main/java/io/jooby/internal/WebSocketMessageImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.jooby.internal;
77

88
import java.lang.reflect.Type;
9+
import java.nio.ByteBuffer;
910

1011
import edu.umd.cs.findbugs.annotations.NonNull;
1112
import edu.umd.cs.findbugs.annotations.Nullable;
@@ -73,4 +74,14 @@ public Value getOrDefault(@NonNull String name, @NonNull String defaultValue) {
7374
public <T> T toNullable(@NonNull Type type) {
7475
return this.to(type);
7576
}
77+
78+
@Override
79+
@NonNull public byte[] bytes() {
80+
return super.bytes();
81+
}
82+
83+
@Override
84+
public @NonNull ByteBuffer byteBuffer() {
85+
return ByteBuffer.wrap(bytes());
86+
}
7687
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3825;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import java.nio.charset.StandardCharsets;
11+
12+
import io.jooby.junit.ServerTest;
13+
import io.jooby.junit.ServerTestRunner;
14+
15+
public class Issue3825 {
16+
@ServerTest
17+
public void shouldHaveAccessToBytes(ServerTestRunner runner) {
18+
runner
19+
.define(
20+
app -> {
21+
app.ws(
22+
"/ws/3825",
23+
(ctx, initializer) -> {
24+
initializer.onMessage(
25+
(ws, message) -> {
26+
ws.send(
27+
">bytes: "
28+
+ new String(message.bytes())
29+
+ "; "
30+
+ new String(message.byteBuffer().array()));
31+
});
32+
});
33+
})
34+
.ready(
35+
client -> {
36+
client.syncWebSocket(
37+
"/ws/3825",
38+
ws -> {
39+
assertEquals(
40+
">bytes: bytes[]; bytes[]",
41+
ws.sendBytes("bytes[]".getBytes(StandardCharsets.UTF_8)));
42+
});
43+
});
44+
}
45+
}

0 commit comments

Comments
 (0)