Issue #8151 - add close with callback to Jetty WebSocket API

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2022-06-17 18:17:49 +10:00
parent ed541d344c
commit 4d217cd75b
4 changed files with 41 additions and 34 deletions

View File

@ -63,6 +63,32 @@ public interface Session extends WebSocketPolicy, Closeable
*/
void close(int statusCode, String reason);
/**
* Send a websocket Close frame, with status code.
* <p>
* This will enqueue a graceful close to the remote endpoint.
*
* @param statusCode the status code
* @param reason the (optional) reason. (can be null for no reason)
* @param callback the callback to track close frame sent (or failed)
* @see StatusCode
* @see #close()
* @see #close(CloseStatus)
* @see #disconnect()
*/
default void close(int statusCode, String reason, WriteCallback callback)
{
try
{
close(statusCode, reason);
callback.writeSuccess();
}
catch (Throwable t)
{
callback.writeFailed(t);
}
}
/**
* Issue a harsh disconnect of the underlying connection.
* <p>

View File

@ -20,7 +20,9 @@ package org.eclipse.jetty.websocket.api;
*/
public interface WriteCallback
{
WriteCallback NOOP = new Adaptor();
WriteCallback NOOP = new WriteCallback()
{
};
/**
* <p>
@ -44,6 +46,7 @@ public interface WriteCallback
{
}
@Deprecated
class Adaptor implements WriteCallback
{
@Override

View File

@ -23,7 +23,6 @@ import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.websocket.api.BatchMode;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WriteCallback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
@ -48,35 +47,6 @@ public class JettyWebSocketRemoteEndpoint implements org.eclipse.jetty.websocket
this.batchMode = batchMode;
}
/**
* Initiate close of the Remote with no status code (no payload)
*
* @since 10.0
*/
public void close()
{
close(StatusCode.NO_CODE, null);
}
/**
* Initiate close of the Remote with specified status code and optional reason phrase
*
* @param statusCode the status code (must be valid and can be sent)
* @param reason optional reason code
* @since 10.0
*/
public void close(int statusCode, String reason)
{
try
{
coreSession.close(statusCode, reason, Callback.NOOP);
}
catch (Throwable t)
{
LOG.trace("IGNORED", t);
}
}
@Override
public void sendString(String text) throws IOException
{

View File

@ -18,6 +18,7 @@ import java.net.SocketAddress;
import java.time.Duration;
import java.util.Objects;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.websocket.api.CloseStatus;
import org.eclipse.jetty.websocket.api.Session;
@ -27,6 +28,7 @@ import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketContainer;
import org.eclipse.jetty.websocket.api.WriteCallback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,19 +55,25 @@ public class WebSocketSession implements Session, SuspendToken, Dumpable
@Override
public void close()
{
remoteEndpoint.close(StatusCode.NORMAL, null);
coreSession.close(StatusCode.NORMAL, null, Callback.NOOP);
}
@Override
public void close(CloseStatus closeStatus)
{
remoteEndpoint.close(closeStatus.getCode(), closeStatus.getPhrase());
coreSession.close(closeStatus.getCode(), closeStatus.getPhrase(), Callback.NOOP);
}
@Override
public void close(int statusCode, String reason)
{
remoteEndpoint.close(statusCode, reason);
coreSession.close(statusCode, reason, Callback.NOOP);
}
@Override
public void close(int statusCode, String reason, WriteCallback callback)
{
coreSession.close(statusCode, reason, Callback.from(callback::writeSuccess, callback::writeFailed));
}
@Override