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 20:17:36 UTC

[wicket] branch wicket-9.x updated: [WICKET-6967] allow sending asynchronous messages via IWebSocketConnection

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

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


The following commit(s) were added to refs/heads/wicket-9.x by this push:
     new abca745  [WICKET-6967] allow sending asynchronous messages via IWebSocketConnection
abca745 is described below

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

    [WICKET-6967] allow sending asynchronous messages via IWebSocketConnection
---
 .../protocol/ws/api/IWebSocketConnection.java      | 50 ++++++++++++++++++++++
 .../ws/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 82cf6e9..7275559 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<Void> 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<Void> 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<Void> 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<Void> 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 1923f49..28dce65 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 @@ abstract class TestWebSocketConnection implements IWebSocketConnection
 		return this;
 	}
 
-	@Override
+    @Override
+    public Future<Void> sendMessageAsync(String message)
+    {
+        return sendMessageAsync(message, -1);
+    }
+
+    @Override
+    public Future<Void> 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 @@ abstract class TestWebSocketConnection implements IWebSocketConnection
 		return this;
 	}
 
-	/**
+    @Override
+    public Future<Void> sendMessageAsync(byte[] message, int offset, int length)
+    {
+        return sendMessageAsync(message, offset, length, -1);
+    }
+
+    @Override
+    public Future<Void> 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 f7e4ca4..4f454ac 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> sendMessageAsync(String message)
+    {
+        checkClosed();
+
+        return session.getAsyncRemote().sendText(message);
+    }
+
+    @Override
+    public Future<Void> 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 class JavaxWebSocketConnection extends AbstractWebSocketConnection
 		return this;
 	}
 
-	private void checkClosed()
+    @Override
+    public Future<Void> sendMessageAsync(byte[] message, int offset, int length)
+    {
+        checkClosed();
+
+        ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
+        return session.getAsyncRemote().sendBinary(buf);
+    }
+
+    @Override
+
+    public Future<Void> 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())
 		{