Add isOpen to WebSocketSession in WebFlux

Closes gh-26043
This commit is contained in:
Rossen Stoyanchev 2020-11-09 14:06:18 +00:00
parent c73cff8bad
commit 6bb3ad793e
6 changed files with 47 additions and 1 deletions

View File

@ -80,6 +80,12 @@ public interface WebSocketSession {
*/
Mono<Void> send(Publisher<WebSocketMessage> messages);
/**
* Whether the underlying connection is open.
* @since 5.3.1
*/
boolean isOpen();
/**
* Close the WebSocket session with {@link CloseStatus#NORMAL}.
*/

View File

@ -113,6 +113,11 @@ public class JettyWebSocketSession extends AbstractListenerWebSocketSession<Sess
return true;
}
@Override
public boolean isOpen() {
return getDelegate().isOpen();
}
@Override
public Mono<Void> close(CloseStatus status) {
getDelegate().close(status.getCode(), status.getReason());

View File

@ -15,10 +15,13 @@
*/
package org.springframework.web.reactive.socket.adapter;
import java.util.function.Consumer;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.Connection;
import reactor.netty.NettyInbound;
import reactor.netty.NettyOutbound;
import reactor.netty.http.websocket.WebsocketInbound;
@ -93,6 +96,13 @@ public class ReactorNettyWebSocketSession
.then();
}
@Override
public boolean isOpen() {
DisposedCallback callback = new DisposedCallback();
getDelegate().getInbound().withConnection(callback);
return callback.isDisposed();
}
@Override
public Mono<Void> close(CloseStatus status) {
// this will notify WebSocketInbound.receiveCloseStatus()
@ -129,4 +139,19 @@ public class ReactorNettyWebSocketSession
}
}
private static class DisposedCallback implements Consumer<Connection> {
private boolean disposed;
public boolean isDisposed() {
return this.disposed;
}
@Override
public void accept(Connection connection) {
this.disposed = connection.isDisposed();
}
}
}

View File

@ -103,6 +103,11 @@ public class StandardWebSocketSession extends AbstractListenerWebSocketSession<S
return true;
}
@Override
public boolean isOpen() {
return getDelegate().isOpen();
}
@Override
public Mono<Void> close(CloseStatus status) {
try {

View File

@ -108,6 +108,11 @@ public class UndertowWebSocketSession extends AbstractListenerWebSocketSession<W
return true;
}
@Override
public boolean isOpen() {
return getDelegate().isOpen();
}
@Override
public Mono<Void> close(CloseStatus status) {
CloseMessage cm = new CloseMessage(status.getCode(), status.getReason());

View File

@ -130,7 +130,7 @@ public interface WebSocketSession extends Closeable {
void sendMessage(WebSocketMessage<?> message) throws IOException;
/**
* Return whether the connection is still open.
* Whether the underlying connection is open.
*/
boolean isOpen();