Example usage for org.springframework.web.reactive.socket WebSocketSession receive

List of usage examples for org.springframework.web.reactive.socket WebSocketSession receive

Introduction

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

Prototype

Flux<WebSocketMessage> receive();

Source Link

Document

Provides access to the stream of inbound messages.

Usage

From source file:top.zhacker.ms.reactor.webflux.controllers.EchoWebSocketHandler.java

@Override
public Mono<Void> handle(WebSocketSession session) {
    // Use retain() for Reactor Netty
    return session
            .send(session.receive().doOnNext(WebSocketMessage::retain).delayElements(Duration.ofSeconds(2)));
}

From source file:org.springframework.cloud.gateway.test.websocket.WebSocketIntegrationTests.java

@Test
public void subProtocol() throws Exception {
    String protocol = "echo-v1";
    String protocol2 = "echo-v2";
    AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
    MonoProcessor<Object> output = MonoProcessor.create();

    client.execute(getUrl("/sub-protocol"), new WebSocketHandler() {
        @Override//from   www  .j a  v a2 s.  c om
        public List<String> getSubProtocols() {
            return Arrays.asList(protocol, protocol2);
        }

        @Override
        public Mono<Void> handle(WebSocketSession session) {
            infoRef.set(session.getHandshakeInfo());
            return session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then();
        }
    }).block(Duration.ofMillis(5000));

    HandshakeInfo info = infoRef.get();
    assertThat(info.getHeaders().getFirst("Upgrade")).isEqualToIgnoringCase("websocket");

    assertThat(info.getHeaders().getFirst("Sec-WebSocket-Protocol")).isEqualTo(protocol);
    assertThat(info.getSubProtocol()).as("Wrong protocol accepted").isEqualTo(protocol);
    assertThat(output.block(Duration.ofSeconds(5))).as("Wrong protocol detected on the server side")
            .isEqualTo(protocol);
}

From source file:org.springframework.web.reactive.socket.WebSocketIntegrationTests.java

@Test
public void subProtocol() throws Exception {
    String protocol = "echo-v1";
    AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
    MonoProcessor<Object> output = MonoProcessor.create();

    this.client.execute(getUrl("/sub-protocol"), new WebSocketHandler() {
        @Override/*  ww  w.  j  a  v  a 2s  .  co m*/
        public List<String> getSubProtocols() {
            return Collections.singletonList(protocol);
        }

        @Override
        public Mono<Void> handle(WebSocketSession session) {
            infoRef.set(session.getHandshakeInfo());
            return session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then();
        }
    }).block(Duration.ofMillis(5000));

    HandshakeInfo info = infoRef.get();
    assertThat(info.getHeaders().getFirst("Upgrade"), Matchers.equalToIgnoringCase("websocket"));
    assertEquals(protocol, info.getHeaders().getFirst("Sec-WebSocket-Protocol"));
    assertEquals("Wrong protocol accepted", protocol, info.getSubProtocol());
    assertEquals("Wrong protocol detected on the server side", protocol, output.block(Duration.ofMillis(5000)));
}