Skip to content

Commit 07d6622

Browse files
committed
[WICKET-6967] allow sending asynchronous messages via IWebSocketConnection
1 parent a37fecc commit 07d6622

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/IWebSocketConnection.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.wicket.protocol.ws.api;
1818

1919
import java.io.IOException;
20+
import java.util.concurrent.Future;
2021

2122
import org.apache.wicket.Application;
2223
import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage;
@@ -55,6 +56,27 @@ public interface IWebSocketConnection
5556
*/
5657
IWebSocketConnection sendMessage(String message) throws IOException;
5758

59+
/**
60+
* Sends a text message to the client in an asynchronous way.
61+
*
62+
* @param message
63+
* the text message
64+
* @return a {@link java.util.concurrent.Future} representing the send operation
65+
*
66+
*/
67+
Future<Void> sendMessageAsync(String message);
68+
69+
/**
70+
* Sends a text message to the client in an asynchronous way.
71+
*
72+
* @param message
73+
* the text message
74+
* @param timeOut
75+
* the timeout for operation
76+
* @return a {@link java.util.concurrent.Future} representing the send operation
77+
*/
78+
Future<Void> sendMessageAsync(String message, long timeOut);
79+
5880
/**
5981
* Sends a binary message to the client.
6082
*
@@ -69,6 +91,34 @@ public interface IWebSocketConnection
6991
*/
7092
IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException;
7193

94+
/**
95+
* Sends a binary message to the client in an asynchronous way.
96+
*
97+
* @param message
98+
* the binary message
99+
* @param offset
100+
* the offset to read from
101+
* @param length
102+
* how much data to read
103+
* @return a {@link java.util.concurrent.Future} representing the send operation
104+
*/
105+
Future<Void> sendMessageAsync(byte[] message, int offset, int length);
106+
107+
/**
108+
* Sends a binary message to the client in an asynchronous way.
109+
*
110+
* @param message
111+
* the binary message
112+
* @param offset
113+
* the offset to read from
114+
* @param length
115+
* how much data to read
116+
* @param timeOut
117+
* * the timeout for operation
118+
* @return a {@link java.util.concurrent.Future} representing the send operation
119+
*/
120+
Future<Void> sendMessageAsync(byte[] message, int offset, int length, long timeOut);
121+
72122
/**
73123
* Broadcasts a push message to the wicket page (and it's components) associated with this
74124
* connection. The components can then send messages or component updates to client by adding

wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/util/tester/TestWebSocketConnection.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.wicket.protocol.ws.util.tester;
1818

1919
import java.io.IOException;
20+
import java.util.concurrent.Future;
2021

2122
import org.apache.wicket.Application;
2223
import org.apache.wicket.protocol.http.WebApplication;
@@ -62,15 +63,43 @@ public IWebSocketConnection sendMessage(String message) throws IOException
6263
return this;
6364
}
6465

65-
@Override
66+
@Override
67+
public Future<Void> sendMessageAsync(String message)
68+
{
69+
return sendMessageAsync(message, -1);
70+
}
71+
72+
@Override
73+
public Future<Void> sendMessageAsync(String message, long timeOut)
74+
{
75+
checkOpenness();
76+
onOutMessage(message);
77+
return null;
78+
}
79+
80+
@Override
6681
public IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException
6782
{
6883
checkOpenness();
6984
onOutMessage(message, offset, length);
7085
return this;
7186
}
7287

73-
/**
88+
@Override
89+
public Future<Void> sendMessageAsync(byte[] message, int offset, int length)
90+
{
91+
return sendMessageAsync(message, offset, length, -1);
92+
}
93+
94+
@Override
95+
public Future<Void> sendMessageAsync(byte[] message, int offset, int length, long timeOut)
96+
{
97+
checkOpenness();
98+
onOutMessage(message, offset, length);
99+
return null;
100+
}
101+
102+
/**
74103
* A callback method that is called when a text message should be send to the client
75104
*
76105
* @param message

wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxWebSocketConnection.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.nio.ByteBuffer;
21+
import java.util.concurrent.Future;
2122

2223
import javax.websocket.CloseReason;
24+
import javax.websocket.RemoteEndpoint;
2325
import javax.websocket.Session;
2426

2527
import org.apache.wicket.protocol.ws.api.AbstractWebSocketConnection;
@@ -82,7 +84,25 @@ public synchronized IWebSocketConnection sendMessage(String message) throws IOEx
8284
return this;
8385
}
8486

85-
@Override
87+
@Override
88+
public Future<Void> sendMessageAsync(String message)
89+
{
90+
checkClosed();
91+
92+
return session.getAsyncRemote().sendText(message);
93+
}
94+
95+
@Override
96+
public Future<Void> sendMessageAsync(String message, long timeOut)
97+
{
98+
checkClosed();
99+
100+
RemoteEndpoint.Async remoteEndpoint = session.getAsyncRemote();
101+
remoteEndpoint.setSendTimeout(timeOut);
102+
return remoteEndpoint.sendText(message);
103+
}
104+
105+
@Override
86106
public synchronized IWebSocketConnection sendMessage(byte[] message, int offset, int length)
87107
throws IOException
88108
{
@@ -93,7 +113,28 @@ public synchronized IWebSocketConnection sendMessage(byte[] message, int offset,
93113
return this;
94114
}
95115

96-
private void checkClosed()
116+
@Override
117+
public Future<Void> sendMessageAsync(byte[] message, int offset, int length)
118+
{
119+
checkClosed();
120+
121+
ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
122+
return session.getAsyncRemote().sendBinary(buf);
123+
}
124+
125+
@Override
126+
127+
public Future<Void> sendMessageAsync(byte[] message, int offset, int length, long timeOut)
128+
{
129+
checkClosed();
130+
131+
RemoteEndpoint.Async remoteEndpoint = session.getAsyncRemote();
132+
remoteEndpoint.setSendTimeout(timeOut);
133+
ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
134+
return remoteEndpoint.sendBinary(buf);
135+
}
136+
137+
private void checkClosed()
97138
{
98139
if (!isOpen())
99140
{

0 commit comments

Comments
 (0)