You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by re...@apache.org on 2022/04/01 19:11:07 UTC

[wicket] 01/01: [WICKET-6967] allow sending asynchronous messages

This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch improvement/reiern70/WICKET-6967
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 0ef90df522c4309cae3923f080b54eaf17dd645d
Author: reiern70 <re...@gmail.com>
AuthorDate: Fri Apr 1 13:10:54 2022 -0600

    [WICKET-6967] allow sending asynchronous messages
---
 .../protocol/ws/api/IWebSocketConnection.java      | 50 ++++++++++++++++++++++
 .../ws/util/tester/TestWebSocketConnection.java    | 34 ++++++++++++++-
 .../ws/javax/JavaxWebSocketConnection.java         | 45 ++++++++++++++++++-
 3 files changed, 125 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 82cf6e9..dea6e60 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.
+     *
+     * @param message
+     *      the text message
+     * @return a {@link java.util.concurrent.Future} representing the send operation
+     *
+     */
+    Future<Void> sendAsynchronousMessage(String message);
+
+    /**
+     * Sends a text message to the client.
+     *
+     * @param message
+     *      the text message
+     * @param timeOut
+     *      the timeout for operation
+     * @return a {@link java.util.concurrent.Future} representing the send operation
+     */
+    Future<Void> sendAsynchronousMessage(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.
+     *
+     * @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<Void> sendAsynchronousMessage(byte[] message, int offset, int length);
+
+    /**
+     * Sends a binary message to the client.
+     *
+     * @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<Void> sendAsynchronousMessage(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 1923f49..fda3245 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,22 @@ abstract class TestWebSocketConnection implements IWebSocketConnection
 		return this;
 	}
 
-	@Override
+    @Override
+    public Future<Void> sendAsynchronousMessage(String message)
+    {
+        checkOpenness();
+        onOutMessage(message);
+        return null;
+    }
+
+    @Override
+    public Future<Void> sendAsynchronousMessage(String message, long timeOut) {
+        checkOpenness();
+        onOutMessage(message);
+        return null;
+    }
+
+    @Override
 	public IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException
 	{
 		checkOpenness();
@@ -70,7 +86,21 @@ abstract class TestWebSocketConnection implements IWebSocketConnection
 		return this;
 	}
 
-	/**
+    @Override
+    public Future<Void> sendAsynchronousMessage(byte[] message, int offset, int length) {
+        checkOpenness();
+        onOutMessage(message, offset, length);
+        return null;
+    }
+
+    @Override
+    public Future<Void> sendAsynchronousMessage(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 f7e4ca4..9466707 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 @@ package org.apache.wicket.protocol.ws.javax;
 
 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 class JavaxWebSocketConnection extends AbstractWebSocketConnection
 		return this;
 	}
 
-	@Override
+    @Override
+    public Future<Void> sendAsynchronousMessage(String message)
+    {
+        checkClosed();
+
+        return session.getAsyncRemote().sendText(message);
+    }
+
+    @Override
+    public Future<Void> sendAsynchronousMessage(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 class JavaxWebSocketConnection extends AbstractWebSocketConnection
 		return this;
 	}
 
-	private void checkClosed()
+    @Override
+    public Future<Void> sendAsynchronousMessage(byte[] message, int offset, int length)
+    {
+        checkClosed();
+
+        ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
+        return session.getAsyncRemote().sendBinary(buf);
+    }
+
+    @Override
+
+    public Future<Void> sendAsynchronousMessage(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())
 		{