Example usage for org.springframework.web.reactive.socket HandshakeInfo getSubProtocol

List of usage examples for org.springframework.web.reactive.socket HandshakeInfo getSubProtocol

Introduction

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

Prototype

@Nullable
public String getSubProtocol() 

Source Link

Document

The sub-protocol negotiated at handshake time, or null if none.

Usage

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/*  w  ww .j a  v  a 2  s  .  co m*/
        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//from  w  ww .j a  v a 2s .  c o 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)));
}