Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WICKET-6967] allow sending asynchronous messages #506

Open
wants to merge 1 commit into
base: wicket-9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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.
*
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 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
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,43 @@ public IWebSocketConnection sendMessage(String message) throws IOException
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();
onOutMessage(message, offset, length);
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
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> 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
{
Expand All @@ -93,7 +113,28 @@ public synchronized IWebSocketConnection sendMessage(byte[] message, int offset,
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())
{
Expand Down