Search in sources :

Example 1 with ReactorNettyWebSocketSession

use of org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession 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)

Example 2 with ReactorNettyWebSocketSession

use of org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession in project spring-framework by spring-projects.

the class ReactorNettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ReactorServerHttpResponse response = (ReactorServerHttpResponse) exchange.getResponse();
    HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
    NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
    return response.getReactorResponse().sendWebsocket(subProtocol.orElse(null), (in, out) -> handler.handle(new ReactorNettyWebSocketSession(in, out, info, bufferFactory)));
}
Also used : ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) ReactorServerHttpResponse(org.springframework.http.server.reactive.ReactorServerHttpResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 3 with ReactorNettyWebSocketSession

use of org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession in project spring-framework by spring-projects.

the class ReactorNettyWebSocketClient method execute.

@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
    String protocols = StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols());
    return getHttpClient().headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)).websocket(buildSpec(protocols)).uri(url.toString()).handle((inbound, outbound) -> {
        HttpHeaders responseHeaders = toHttpHeaders(inbound);
        String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
        HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
        NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc());
        WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory, getMaxFramePayloadLength());
        if (logger.isDebugEnabled()) {
            logger.debug("Started session '" + session.getId() + "' for " + url);
        }
        return handler.handle(session).checkpoint(url + " [ReactorNettyWebSocketClient]");
    }).doOnRequest(n -> {
        if (logger.isDebugEnabled()) {
            logger.debug("Connecting to " + url);
        }
    }).next();
}
Also used : NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) WebsocketClientSpec(reactor.netty.http.client.WebsocketClientSpec) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) Supplier(java.util.function.Supplier) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound) Log(org.apache.commons.logging.Log) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) LogFactory(org.apache.commons.logging.LogFactory) HttpClient(reactor.netty.http.client.HttpClient) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) HttpHeaders(org.springframework.http.HttpHeaders) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)

Aggregations

NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)3 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)3 ReactorNettyWebSocketSession (org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)3 URI (java.net.URI)2 Supplier (java.util.function.Supplier)2 Nullable (org.springframework.lang.Nullable)2 Assert (org.springframework.util.Assert)2 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)2 Mono (reactor.core.publisher.Mono)2 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ReactorServerHttpResponse (org.springframework.http.server.reactive.ReactorServerHttpResponse)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServerHttpResponseDecorator (org.springframework.http.server.reactive.ServerHttpResponseDecorator)1 StringUtils (org.springframework.util.StringUtils)1 WebSocketSession (org.springframework.web.reactive.socket.WebSocketSession)1 RequestUpgradeStrategy (org.springframework.web.reactive.socket.server.RequestUpgradeStrategy)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 HttpClient (reactor.netty.http.client.HttpClient)1