Search in sources :

Example 1 with WebsocketServerSpec

use of reactor.netty.http.server.WebsocketServerSpec in project spring-framework by spring-projects.

the class ReactorNettyRequestUpgradeStrategyTests method defaultWebSocketSpecBuilderIsUniquePerRequest.

// gh-25315
@Test
void defaultWebSocketSpecBuilderIsUniquePerRequest() {
    ReactorNettyRequestUpgradeStrategy strategy = new ReactorNettyRequestUpgradeStrategy();
    WebsocketServerSpec spec1 = strategy.buildSpec("p1");
    WebsocketServerSpec spec2 = strategy.getWebsocketServerSpec();
    assertThat(spec1.protocols()).isEqualTo("p1");
    assertThat(spec2.protocols()).isNull();
}
Also used : WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) Test(org.junit.jupiter.api.Test)

Example 2 with WebsocketServerSpec

use of reactor.netty.http.server.WebsocketServerSpec in project spring-framework by spring-projects.

the class ReactorNettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpResponse response = exchange.getResponse();
    HttpServerResponse reactorResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
    URI uri = exchange.getRequest().getURI();
    // Trigger WebFlux preCommit actions and upgrade
    return response.setComplete().then(Mono.defer(() -> {
        WebsocketServerSpec spec = buildSpec(subProtocol);
        return reactorResponse.sendWebsocket((in, out) -> {
            ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession(in, out, handshakeInfo, bufferFactory, spec.maxFramePayloadLength());
            return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
        }, spec);
    }));
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) Supplier(java.util.function.Supplier) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) Assert(org.springframework.util.Assert) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) URI(java.net.URI) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

WebsocketServerSpec (reactor.netty.http.server.WebsocketServerSpec)2 URI (java.net.URI)1 Supplier (java.util.function.Supplier)1 Test (org.junit.jupiter.api.Test)1 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServerHttpResponseDecorator (org.springframework.http.server.reactive.ServerHttpResponseDecorator)1 Nullable (org.springframework.lang.Nullable)1 Assert (org.springframework.util.Assert)1 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)1 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)1 ReactorNettyWebSocketSession (org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)1 RequestUpgradeStrategy (org.springframework.web.reactive.socket.server.RequestUpgradeStrategy)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 Mono (reactor.core.publisher.Mono)1 HttpServerResponse (reactor.netty.http.server.HttpServerResponse)1