Example usage for org.springframework.web.reactive.socket WebSocketHandler handle

List of usage examples for org.springframework.web.reactive.socket WebSocketHandler handle

Introduction

In this page you can find the example usage for org.springframework.web.reactive.socket WebSocketHandler handle.

Prototype

Mono<Void> handle(WebSocketSession session);

Source Link

Document

Invoked when a new WebSocket connection is established, and allows handling of the session.

Usage

From source file:org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient.java

@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
    return getHttpClient().headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders))
            .websocket(StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols()))
            .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);
                if (logger.isDebugEnabled()) {
                    logger.debug("Started session '" + session.getId() + "' for " + url);
                }/* w  w w  . j  a v a 2s.c  om*/
                return handler.handle(session);
            }).doOnRequest(n -> {
                if (logger.isDebugEnabled()) {
                    logger.debug("Connecting to " + url);
                }
            }).next();
}

From source file:org.springframework.web.reactive.socket.client.UndertowWebSocketClient.java

private void handleChannel(URI url, WebSocketHandler handler, MonoProcessor<Void> completion,
        DefaultNegotiation negotiation, WebSocketChannel channel) {

    HandshakeInfo info = createHandshakeInfo(url, negotiation);
    UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, this.bufferFactory,
            completion);//www. ja v a2s  . co  m
    UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);

    channel.getReceiveSetter().set(adapter);
    channel.resumeReceives();

    handler.handle(session).subscribe(session);
}