Skip to content

Commit 0ef90df

Browse files
committed
[WICKET-6967] allow sending asynchronous messages
1 parent a37fecc commit 0ef90df

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed

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

+50
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.
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> sendAsynchronousMessage(String message);
68+
69+
/**
70+
* Sends a text message to the client.
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> sendAsynchronousMessage(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.
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> sendAsynchronousMessage(byte[] message, int offset, int length);
106+
107+
/**
108+
* Sends a binary message to the client.
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> sendAsynchronousMessage(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

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

+32-2
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,44 @@ public IWebSocketConnection sendMessage(String message) throws IOException
6263
return this;
6364
}
6465

65-
@Override
66+
@Override
67+
public Future<Void> sendAsynchronousMessage(String message)
68+
{
69+
checkOpenness();
70+
onOutMessage(message);
71+
return null;
72+
}
73+
74+
@Override
75+
public Future<Void> sendAsynchronousMessage(String message, long timeOut) {
76+
checkOpenness();
77+
onOutMessage(message);
78+
return null;
79+
}
80+
81+
@Override
6682
public IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException
6783
{
6884
checkOpenness();
6985
onOutMessage(message, offset, length);
7086
return this;
7187
}
7288

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

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

+43-2
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> sendAsynchronousMessage(String message)
89+
{
90+
checkClosed();
91+
92+
return session.getAsyncRemote().sendText(message);
93+
}
94+
95+
@Override
96+
public Future<Void> sendAsynchronousMessage(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> sendAsynchronousMessage(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> sendAsynchronousMessage(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)