Search in sources :

Example 1 with StandardWebSocketSession

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

the class StandardWebSocketClient method createEndpoint.

private StandardWebSocketHandlerAdapter createEndpoint(URI url, WebSocketHandler handler, MonoProcessor<Void> completion, DefaultConfigurator configurator) {
    return new StandardWebSocketHandlerAdapter(handler, session -> {
        HttpHeaders responseHeaders = configurator.getResponseHeaders();
        HandshakeInfo info = afterHandshake(url, responseHeaders);
        return new StandardWebSocketSession(session, info, this.bufferFactory, completion);
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 2 with StandardWebSocketSession

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

the class TomcatRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new StandardWebSocketSession(session, info, factory);
    });
    String requestURI = servletRequest.getRequestURI();
    DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
    config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList()));
    try {
        WsServerContainer container = getContainer(servletRequest);
        container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
    } catch (ServletException | IOException ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Endpoint(javax.websocket.Endpoint) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) Collections(java.util.Collections) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)2 StandardWebSocketHandlerAdapter (org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter)2 StandardWebSocketSession (org.springframework.web.reactive.socket.adapter.StandardWebSocketSession)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Endpoint (javax.websocket.Endpoint)1 WsServerContainer (org.apache.tomcat.websocket.server.WsServerContainer)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)1