@ -27,7 +27,9 @@ import io.undertow.websockets.core.WebSockets;
import reactor.core.publisher.Mono ;
import reactor.core.publisher.Mono ;
import reactor.core.publisher.MonoProcessor ;
import reactor.core.publisher.MonoProcessor ;
import org.springframework.core.io.buffer.DataBuffer ;
import org.springframework.core.io.buffer.DataBufferFactory ;
import org.springframework.core.io.buffer.DataBufferFactory ;
import org.springframework.core.io.buffer.DataBufferUtils ;
import org.springframework.lang.Nullable ;
import org.springframework.lang.Nullable ;
import org.springframework.util.ObjectUtils ;
import org.springframework.util.ObjectUtils ;
import org.springframework.web.reactive.socket.CloseStatus ;
import org.springframework.web.reactive.socket.CloseStatus ;
@ -78,19 +80,19 @@ public class UndertowWebSocketSession extends AbstractListenerWebSocketSession<W
if ( WebSocketMessage . Type . TEXT . equals ( message . getType ( ) ) ) {
if ( WebSocketMessage . Type . TEXT . equals ( message . getType ( ) ) ) {
getSendProcessor ( ) . setReadyToSend ( false ) ;
getSendProcessor ( ) . setReadyToSend ( false ) ;
String text = new String ( buffer . array ( ) , StandardCharsets . UTF_8 ) ;
String text = new String ( buffer . array ( ) , StandardCharsets . UTF_8 ) ;
WebSockets . sendText ( text , getDelegate ( ) , new SendProcessorCallback ( ) ) ;
WebSockets . sendText ( text , getDelegate ( ) , new SendProcessorCallback ( message . getPayload ( ) ) ) ;
}
}
else if ( WebSocketMessage . Type . BINARY . equals ( message . getType ( ) ) ) {
else if ( WebSocketMessage . Type . BINARY . equals ( message . getType ( ) ) ) {
getSendProcessor ( ) . setReadyToSend ( false ) ;
getSendProcessor ( ) . setReadyToSend ( false ) ;
WebSockets . sendBinary ( buffer , getDelegate ( ) , new SendProcessorCallback ( ) ) ;
WebSockets . sendBinary ( buffer , getDelegate ( ) , new SendProcessorCallback ( message . getPayload ( ) ) ) ;
}
}
else if ( WebSocketMessage . Type . PING . equals ( message . getType ( ) ) ) {
else if ( WebSocketMessage . Type . PING . equals ( message . getType ( ) ) ) {
getSendProcessor ( ) . setReadyToSend ( false ) ;
getSendProcessor ( ) . setReadyToSend ( false ) ;
WebSockets . sendPing ( buffer , getDelegate ( ) , new SendProcessorCallback ( ) ) ;
WebSockets . sendPing ( buffer , getDelegate ( ) , new SendProcessorCallback ( message . getPayload ( ) ) ) ;
}
}
else if ( WebSocketMessage . Type . PONG . equals ( message . getType ( ) ) ) {
else if ( WebSocketMessage . Type . PONG . equals ( message . getType ( ) ) ) {
getSendProcessor ( ) . setReadyToSend ( false ) ;
getSendProcessor ( ) . setReadyToSend ( false ) ;
WebSockets . sendPong ( buffer , getDelegate ( ) , new SendProcessorCallback ( ) ) ;
WebSockets . sendPong ( buffer , getDelegate ( ) , new SendProcessorCallback ( message . getPayload ( ) ) ) ;
}
}
else {
else {
throw new IllegalArgumentException ( "Unexpected message type: " + message . getType ( ) ) ;
throw new IllegalArgumentException ( "Unexpected message type: " + message . getType ( ) ) ;
@ -110,14 +112,22 @@ public class UndertowWebSocketSession extends AbstractListenerWebSocketSession<W
private final class SendProcessorCallback implements WebSocketCallback < Void > {
private final class SendProcessorCallback implements WebSocketCallback < Void > {
private final DataBuffer payload ;
SendProcessorCallback ( DataBuffer payload ) {
this . payload = payload ;
}
@Override
@Override
public void complete ( WebSocketChannel channel , Void context ) {
public void complete ( WebSocketChannel channel , Void context ) {
DataBufferUtils . release ( this . payload ) ;
getSendProcessor ( ) . setReadyToSend ( true ) ;
getSendProcessor ( ) . setReadyToSend ( true ) ;
getSendProcessor ( ) . onWritePossible ( ) ;
getSendProcessor ( ) . onWritePossible ( ) ;
}
}
@Override
@Override
public void onError ( WebSocketChannel channel , Void context , Throwable throwable ) {
public void onError ( WebSocketChannel channel , Void context , Throwable throwable ) {
DataBufferUtils . release ( this . payload ) ;
getSendProcessor ( ) . cancel ( ) ;
getSendProcessor ( ) . cancel ( ) ;
getSendProcessor ( ) . onError ( throwable ) ;
getSendProcessor ( ) . onError ( throwable ) ;
}
}