Skip to content

Commit

Permalink
[WICKET-6967] allow sending asynchronous messages
Browse files Browse the repository at this point in the history
  • Loading branch information
reiern70 committed Apr 1, 2022
1 parent a37fecc commit 0ef90df
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,15 +63,44 @@ public IWebSocketConnection sendMessage(String message) throws IOException
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();
onOutMessage(message, offset, length);
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +84,25 @@ public synchronized IWebSocketConnection sendMessage(String message) throws IOEx
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
{
Expand All @@ -93,7 +113,28 @@ public synchronized IWebSocketConnection sendMessage(byte[] message, int offset,
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())
{
Expand Down

0 comments on commit 0ef90df

Please sign in to comment.