From 07d66229908b106af9836a874b3255a539433bf6 Mon Sep 17 00:00:00 2001 From: reiern70 Date: Fri, 1 Apr 2022 13:10:54 -0600 Subject: [PATCH] [WICKET-6967] allow sending asynchronous messages via IWebSocketConnection --- .../protocol/ws/api/IWebSocketConnection.java | 50 +++++++++++++++++++ .../util/tester/TestWebSocketConnection.java | 33 +++++++++++- .../ws/javax/JavaxWebSocketConnection.java | 45 ++++++++++++++++- 3 files changed, 124 insertions(+), 4 deletions(-) diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/IWebSocketConnection.java b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/IWebSocketConnection.java index 82cf6e9fd5b..7275559bd50 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/IWebSocketConnection.java +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/IWebSocketConnection.java @@ -17,6 +17,7 @@ package org.apache.wicket.protocol.ws.api; import java.io.IOException; +import java.util.concurrent.Future; import org.apache.wicket.Application; import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage; @@ -55,6 +56,27 @@ public interface IWebSocketConnection */ IWebSocketConnection sendMessage(String message) throws IOException; + /** + * Sends a text message to the client in an asynchronous way. + * + * @param message + * the text message + * @return a {@link java.util.concurrent.Future} representing the send operation + * + */ + Future sendMessageAsync(String message); + + /** + * Sends a text message to the client in an asynchronous way. + * + * @param message + * the text message + * @param timeOut + * the timeout for operation + * @return a {@link java.util.concurrent.Future} representing the send operation + */ + Future sendMessageAsync(String message, long timeOut); + /** * Sends a binary message to the client. * @@ -69,6 +91,34 @@ public interface IWebSocketConnection */ IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException; + /** + * Sends a binary message to the client in an asynchronous way. + * + * @param message + * the binary message + * @param offset + * the offset to read from + * @param length + * how much data to read + * @return a {@link java.util.concurrent.Future} representing the send operation + */ + Future sendMessageAsync(byte[] message, int offset, int length); + + /** + * Sends a binary message to the client in an asynchronous way. + * + * @param message + * the binary message + * @param offset + * the offset to read from + * @param length + * how much data to read + * @param timeOut + * * the timeout for operation + * @return a {@link java.util.concurrent.Future} representing the send operation + */ + Future sendMessageAsync(byte[] message, int offset, int length, long timeOut); + /** * Broadcasts a push message to the wicket page (and it's components) associated with this * connection. The components can then send messages or component updates to client by adding diff --git a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/util/tester/TestWebSocketConnection.java b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/util/tester/TestWebSocketConnection.java index 1923f493191..28dce651a5b 100644 --- a/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/util/tester/TestWebSocketConnection.java +++ b/wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/util/tester/TestWebSocketConnection.java @@ -17,6 +17,7 @@ package org.apache.wicket.protocol.ws.util.tester; import java.io.IOException; +import java.util.concurrent.Future; import org.apache.wicket.Application; import org.apache.wicket.protocol.http.WebApplication; @@ -62,7 +63,21 @@ public IWebSocketConnection sendMessage(String message) throws IOException return this; } - @Override + @Override + public Future sendMessageAsync(String message) + { + return sendMessageAsync(message, -1); + } + + @Override + public Future sendMessageAsync(String message, long timeOut) + { + checkOpenness(); + onOutMessage(message); + return null; + } + + @Override public IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException { checkOpenness(); @@ -70,7 +85,21 @@ public IWebSocketConnection sendMessage(byte[] message, int offset, int length) return this; } - /** + @Override + public Future sendMessageAsync(byte[] message, int offset, int length) + { + return sendMessageAsync(message, offset, length, -1); + } + + @Override + public Future sendMessageAsync(byte[] message, int offset, int length, long timeOut) + { + checkOpenness(); + onOutMessage(message, offset, length); + return null; + } + + /** * A callback method that is called when a text message should be send to the client * * @param message diff --git a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxWebSocketConnection.java b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxWebSocketConnection.java index f7e4ca440b4..4f454ac326c 100644 --- a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxWebSocketConnection.java +++ b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxWebSocketConnection.java @@ -18,8 +18,10 @@ import java.io.IOException; import java.nio.ByteBuffer; +import java.util.concurrent.Future; import javax.websocket.CloseReason; +import javax.websocket.RemoteEndpoint; import javax.websocket.Session; import org.apache.wicket.protocol.ws.api.AbstractWebSocketConnection; @@ -82,7 +84,25 @@ public synchronized IWebSocketConnection sendMessage(String message) throws IOEx return this; } - @Override + @Override + public Future sendMessageAsync(String message) + { + checkClosed(); + + return session.getAsyncRemote().sendText(message); + } + + @Override + public Future sendMessageAsync(String message, long timeOut) + { + checkClosed(); + + RemoteEndpoint.Async remoteEndpoint = session.getAsyncRemote(); + remoteEndpoint.setSendTimeout(timeOut); + return remoteEndpoint.sendText(message); + } + + @Override public synchronized IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException { @@ -93,7 +113,28 @@ public synchronized IWebSocketConnection sendMessage(byte[] message, int offset, return this; } - private void checkClosed() + @Override + public Future sendMessageAsync(byte[] message, int offset, int length) + { + checkClosed(); + + ByteBuffer buf = ByteBuffer.wrap(message, offset, length); + return session.getAsyncRemote().sendBinary(buf); + } + + @Override + + public Future sendMessageAsync(byte[] message, int offset, int length, long timeOut) + { + checkClosed(); + + RemoteEndpoint.Async remoteEndpoint = session.getAsyncRemote(); + remoteEndpoint.setSendTimeout(timeOut); + ByteBuffer buf = ByteBuffer.wrap(message, offset, length); + return remoteEndpoint.sendBinary(buf); + } + + private void checkClosed() { if (!isOpen()) {