Skip to content

Commit 2c40dee

Browse files
committed
WebSocket: Allow to configure websocket size
- Fix jooby-project#2566
1 parent 198ea20 commit 2c40dee

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

docs/asciidoc/websocket.adoc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
== Web Sockets
22

3-
Jooby supports https://developer.mozilla.org/es/docs/Web/API/WebSockets_API[WebSockets].
4-
A javadoc:WebSocket[] is registered like any other handler:
3+
Adding a javadoc:WebSocket[]:
54

65
.WebSocket
76
[source,java,role="primary"]
@@ -170,7 +169,9 @@ import io.jooby.json.JacksonModule;
170169
Structure messages depends/requires a javadoc:MessageDecoder[] and jadoc:MessageEncoder[]. In this
171170
example both are provided by the JacksonModule.
172171

173-
=== Connection Timeouts
172+
=== Options
173+
174+
==== Connection Timeouts
174175
By default Jooby will timeout idle connections that have no activity after 5 minutes. You can
175176
control this behaviour by setting the `websocket.idleTimeout` property:
176177

@@ -181,3 +182,15 @@ websocket.idleTimeout = 1h
181182
----
182183

183184
See https://github.com/lightbend/config/blob/master/HOCON.md#duration-format[duration format]
185+
186+
==== Max size
187+
188+
Max size is set to `128K` you can override it like:
189+
190+
.application.conf
191+
[source, properties]
192+
----
193+
websocket.maxSize = 128K
194+
----
195+
196+
See https://github.com/lightbend/config/blob/master/HOCON.md#size-in-bytes-format[sizes in bytes]

jooby/src/main/java/io/jooby/WebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ interface OnError {
108108
void onError(@Nonnull WebSocket ws, @Nonnull Throwable cause);
109109
}
110110

111-
/** Max message size for websocket (13k). */
111+
/** Max message size for websocket (128K). */
112112
int MAX_BUFFER_SIZE = 131072;
113113

114114
/**

modules/jooby-jetty/src/main/java/io/jooby/jetty/Jetty.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,14 @@ public class Jetty extends io.jooby.Server.Base {
186186
context.setHandler(handler);
187187
}
188188
/* ********************************* WebSocket *************************************/
189+
Config conf = application.getConfig();
190+
int maxSize = conf.hasPath("websocket.maxSize")
191+
? conf.getBytes("websocket.maxSize").intValue()
192+
: WebSocket.MAX_BUFFER_SIZE;
189193
context.setAttribute(DecoratedObjectFactory.ATTR, new DecoratedObjectFactory());
190194
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
191-
policy.setMaxTextMessageBufferSize(WebSocket.MAX_BUFFER_SIZE);
192-
policy.setMaxTextMessageSize(WebSocket.MAX_BUFFER_SIZE);
193-
Config conf = application.getConfig();
195+
policy.setMaxTextMessageBufferSize(maxSize);
196+
policy.setMaxTextMessageSize(maxSize);
194197
long timeout = conf.hasPath("websocket.idleTimeout")
195198
? conf.getDuration("websocket.idleTimeout", TimeUnit.MILLISECONDS)
196199
: TimeUnit.MINUTES.toMillis(5);

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,16 @@ boolean isHttpGet() {
373373
@Nonnull @Override public Context upgrade(WebSocket.Initializer handler) {
374374
try {
375375
responseStarted = true;
376+
Config conf = getRouter().getConfig();
377+
int maxSize = conf.hasPath("websocket.maxSize")
378+
? conf.getBytes("websocket.maxSize").intValue()
379+
: WebSocket.MAX_BUFFER_SIZE;
376380
String webSocketURL = getProtocol() + "://" + req.headers().get(HttpHeaderNames.HOST) + path;
377381
WebSocketDecoderConfig config = WebSocketDecoderConfig.newBuilder()
378382
.allowExtensions(true)
379383
.allowMaskMismatch(false)
380384
.withUTF8Validator(false)
381-
.maxFramePayloadLength(WebSocket.MAX_BUFFER_SIZE)
385+
.maxFramePayloadLength(maxSize)
382386
.build();
383387
webSocket = new NettyWebSocket(this);
384388
handler.init(Context.readOnly(this), webSocket);
@@ -389,7 +393,6 @@ boolean isHttpGet() {
389393
WebSocketServerHandshaker handshaker = factory.newHandshaker(webSocketRequest);
390394
handshaker.handshake(ctx.channel(), webSocketRequest);
391395
webSocket.fireConnect();
392-
Config conf = getRouter().getConfig();
393396
long timeout = conf.hasPath("websocket.idleTimeout")
394397
? conf.getDuration("websocket.idleTimeout", MILLISECONDS)
395398
: MINUTES.toMillis(5);

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowWebSocket.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,26 @@ public class UtowWebSocket extends AbstractReceiveListener
5656
private String key;
5757
private CountDownLatch ready = new CountDownLatch(1);
5858
private AtomicBoolean open = new AtomicBoolean(false);
59+
private int maxSize;
5960

6061
public UtowWebSocket(UtowContext ctx, WebSocketChannel channel) {
6162
this.ctx = ctx;
6263
this.channel = channel;
6364
this.dispatch = !ctx.isInIoThread();
6465
this.key = ctx.getRoute().getPattern();
66+
67+
Config conf = ctx.getRouter().getConfig();
68+
maxSize = conf.hasPath("websocket.maxSize")
69+
? conf.getBytes("websocket.maxSize").intValue()
70+
: WebSocket.MAX_BUFFER_SIZE;
6571
}
6672

6773
@Override protected long getMaxTextBufferSize() {
68-
return MAX_BUFFER_SIZE;
74+
return maxSize;
6975
}
7076

7177
@Override protected long getMaxBinaryBufferSize() {
72-
return MAX_BUFFER_SIZE;
78+
return maxSize;
7379
}
7480

7581
@Nonnull @Override public Context getContext() {

0 commit comments

Comments
 (0)