|
| 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.i3814; |
| 7 | + |
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | + |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +import io.jooby.junit.ServerTest; |
| 14 | +import io.jooby.junit.ServerTestRunner; |
| 15 | +import okhttp3.Response; |
| 16 | +import okhttp3.WebSocket; |
| 17 | +import okhttp3.WebSocketListener; |
| 18 | +import okio.ByteString; |
| 19 | + |
| 20 | +public class Issue3814 { |
| 21 | + |
| 22 | + @ServerTest |
| 23 | + public void shouldJettyWebSocketWorks(ServerTestRunner runner) { |
| 24 | + int messageCount = 10; |
| 25 | + var latch = new CountDownLatch(messageCount); |
| 26 | + runner |
| 27 | + .define( |
| 28 | + app -> { |
| 29 | + app.ws( |
| 30 | + "/3814", |
| 31 | + (ctx, initializer) -> { |
| 32 | + initializer.onMessage( |
| 33 | + (ws, message) -> { |
| 34 | + latch.countDown(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + }) |
| 38 | + .ready( |
| 39 | + client -> { |
| 40 | + client.webSocket( |
| 41 | + "/3814", |
| 42 | + new WebSocketListener() { |
| 43 | + @Override |
| 44 | + public void onOpen(@NotNull WebSocket ws, @NotNull Response response) { |
| 45 | + for (int i = 0; i < messageCount; i++) { |
| 46 | + ws.send(">" + i); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) { |
| 52 | + super.onMessage(webSocket, text); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) { |
| 57 | + super.onMessage(webSocket, bytes); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onClosing( |
| 62 | + @NotNull WebSocket webSocket, int code, @NotNull String reason) { |
| 63 | + super.onClosing(webSocket, code, reason); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onClosed( |
| 68 | + @NotNull WebSocket webSocket, int code, @NotNull String reason) { |
| 69 | + super.onClosed(webSocket, code, reason); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onFailure( |
| 74 | + @NotNull WebSocket webSocket, |
| 75 | + @NotNull Throwable t, |
| 76 | + @Nullable Response response) { |
| 77 | + super.onFailure(webSocket, t, response); |
| 78 | + } |
| 79 | + }); |
| 80 | + latch.await(); |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments