Skip to content

Commit cb26621

Browse files
committed
allow use of Connection interface in callback arguments
1 parent 029bb77 commit cb26621

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/ConnectionCallbackArgument.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ class ConnectionCallbackArgument implements CallbackArgument {
1212
public boolean matches(ParameterContext context) {
1313
DotName paramTypeName = context.parameter().type().name();
1414
if (context.callbackTarget() == Target.SERVER) {
15-
if (WebSocketDotNames.WEB_SOCKET_CONNECTION.equals(paramTypeName)) {
15+
if (WebSocketDotNames.WEB_SOCKET_CONNECTION.equals(paramTypeName)
16+
|| WebSocketDotNames.CONNECTION.equals(paramTypeName)) {
1617
return true;
1718
} else if (WebSocketDotNames.WEB_SOCKET_CLIENT_CONNECTION.equals(paramTypeName)) {
1819
throw new WebSocketException("@WebSocket callback method may not accept WebSocketClientConnection");
1920
}
2021
} else if (context.callbackTarget() == Target.CLIENT) {
21-
if (WebSocketDotNames.WEB_SOCKET_CLIENT_CONNECTION.equals(paramTypeName)) {
22+
if (WebSocketDotNames.WEB_SOCKET_CLIENT_CONNECTION.equals(paramTypeName)
23+
|| WebSocketDotNames.CONNECTION.equals(paramTypeName)) {
2224
return true;
2325
} else if (WebSocketDotNames.WEB_SOCKET_CONNECTION.equals(paramTypeName)) {
2426
throw new WebSocketException("@WebSocketClient callback method may not accept WebSocketConnection");

extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/WebSocketDotNames.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.jboss.jandex.DotName;
66

77
import io.quarkus.websockets.next.CloseReason;
8+
import io.quarkus.websockets.next.Connection;
89
import io.quarkus.websockets.next.HandshakeRequest;
910
import io.quarkus.websockets.next.OnBinaryMessage;
1011
import io.quarkus.websockets.next.OnClose;
@@ -32,6 +33,7 @@ final class WebSocketDotNames {
3233

3334
static final DotName WEB_SOCKET = DotName.createSimple(WebSocket.class);
3435
static final DotName WEB_SOCKET_CLIENT = DotName.createSimple(WebSocketClient.class);
36+
static final DotName CONNECTION = DotName.createSimple(Connection.class);
3537
static final DotName WEB_SOCKET_CONNECTION = DotName.createSimple(WebSocketConnection.class);
3638
static final DotName WEB_SOCKET_CLIENT_CONNECTION = DotName.createSimple(WebSocketClientConnection.class);
3739
static final DotName WEB_SOCKET_CONNECTOR = DotName.createSimple(WebSocketConnector.class);

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/args/ConnectionArgumentTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.websockets.next.test.args;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
45

56
import java.net.URI;
67

@@ -11,6 +12,8 @@
1112

1213
import io.quarkus.test.QuarkusUnitTest;
1314
import io.quarkus.test.common.http.TestHTTPResource;
15+
import io.quarkus.websockets.next.Connection;
16+
import io.quarkus.websockets.next.OnOpen;
1417
import io.quarkus.websockets.next.OnTextMessage;
1518
import io.quarkus.websockets.next.WebSocket;
1619
import io.quarkus.websockets.next.WebSocketConnection;
@@ -52,6 +55,11 @@ public static class Echo {
5255
@Inject
5356
WebSocketConnection c;
5457

58+
@OnOpen
59+
void connect(Connection connection) {
60+
assertInstanceOf(WebSocketConnection.class, connection);
61+
}
62+
5563
@OnTextMessage
5664
Uni<Void> process(WebSocketConnection connection, String message) throws InterruptedException {
5765
assertEquals(c.id(), connection.id());

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.quarkus.websockets.next.test.client;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import java.net.URI;
78
import java.util.List;
89
import java.util.concurrent.CopyOnWriteArrayList;
910
import java.util.concurrent.CountDownLatch;
1011
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.atomic.AtomicReference;
1113

1214
import jakarta.inject.Inject;
1315

@@ -16,6 +18,7 @@
1618

1719
import io.quarkus.test.QuarkusUnitTest;
1820
import io.quarkus.test.common.http.TestHTTPResource;
21+
import io.quarkus.websockets.next.Connection;
1922
import io.quarkus.websockets.next.OnClose;
2023
import io.quarkus.websockets.next.OnOpen;
2124
import io.quarkus.websockets.next.OnPingMessage;
@@ -50,6 +53,8 @@ void testClient() throws InterruptedException {
5053
// The value will be encoded automatically
5154
.pathParam("name", "Lu=")
5255
.connectAndAwait();
56+
assertTrue(ClientEndpoint.OPEN_LATCH.await(5, TimeUnit.SECONDS));
57+
assertInstanceOf(WebSocketClientConnection.class, ClientEndpoint.CONNECTION.get());
5358
assertEquals("Lu=", connection.pathParam("name"));
5459
connection.sendPingAndAwait(ping);
5560
connection.sendTextAndAwait("Hi!");
@@ -103,6 +108,10 @@ void close() {
103108
@WebSocketClient(path = "/endpoint/{name}")
104109
public static class ClientEndpoint {
105110

111+
static final CountDownLatch OPEN_LATCH = new CountDownLatch(1);
112+
113+
static final AtomicReference<Connection> CONNECTION = new AtomicReference<>();
114+
106115
static final CountDownLatch PING_LATCH = new CountDownLatch(1);
107116

108117
static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(2);
@@ -111,6 +120,12 @@ public static class ClientEndpoint {
111120

112121
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1);
113122

123+
@OnOpen
124+
void onOpen(Connection connection) {
125+
CONNECTION.set(connection);
126+
OPEN_LATCH.countDown();
127+
}
128+
114129
@OnTextMessage
115130
void onMessage(@PathParam String name, String message, WebSocketClientConnection connection) {
116131
if (!name.equals(connection.pathParam("name"))) {

0 commit comments

Comments
 (0)