Search in sources :

Example 1 with WebSocketMessage

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

the class StandardWebSocketHandlerAdapter method toMessage.

private <T> WebSocketMessage toMessage(T message) {
    WebSocketSession session = this.delegateSession;
    Assert.state(session != null, "Cannot create message without a session");
    if (message instanceof String) {
        byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
        return new WebSocketMessage(Type.TEXT, session.bufferFactory().wrap(bytes));
    } else if (message instanceof ByteBuffer) {
        DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
        return new WebSocketMessage(Type.BINARY, buffer);
    } else if (message instanceof PongMessage) {
        DataBuffer buffer = session.bufferFactory().wrap(((PongMessage) message).getApplicationData());
        return new WebSocketMessage(Type.PONG, buffer);
    } else {
        throw new IllegalArgumentException("Unexpected message type: " + message);
    }
}
Also used : WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) PongMessage(javax.websocket.PongMessage) ByteBuffer(java.nio.ByteBuffer) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with WebSocketMessage

use of org.springframework.web.reactive.socket.WebSocketMessage in project tutorials by eugenp.

the class ReactiveJavaClientWebSocket method main.

public static void main(String[] args) throws InterruptedException {
    WebSocketClient client = new ReactorNettyWebSocketClient();
    client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-spring-reactive-client-websocket"))).thenMany(session.receive().map(WebSocketMessage::getPayloadAsText).log()).then()).block(Duration.ofSeconds(10L));
}
Also used : ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) Duration(java.time.Duration) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) Mono(reactor.core.publisher.Mono) URI(java.net.URI) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient)

Example 3 with WebSocketMessage

use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-cloud-gateway by spring-cloud.

the class WebSocketIntegrationTests method subProtocol.

@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
        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);
}
Also used : WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) Test(org.junit.Test)

Example 4 with WebSocketMessage

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

the class AbstractWebSocketSession method textMessage.

// WebSocketMessage factory methods
@Override
public WebSocketMessage textMessage(String payload) {
    byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = bufferFactory().wrap(bytes);
    return new WebSocketMessage(WebSocketMessage.Type.TEXT, buffer);
}
Also used : WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 5 with WebSocketMessage

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

the class JettyWebSocketHandlerAdapter method onWebSocketText.

@OnWebSocketMessage
public void onWebSocketText(String message) {
    if (this.delegateSession != null) {
        WebSocketMessage webSocketMessage = toMessage(Type.TEXT, message);
        this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
    }
}
Also used : OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Aggregations

WebSocketMessage (org.springframework.web.reactive.socket.WebSocketMessage)10 ByteBuffer (java.nio.ByteBuffer)4 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)4 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 WebSocketSession (org.springframework.web.reactive.socket.WebSocketSession)3 URI (java.net.URI)2 Duration (java.time.Duration)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 Server (com.netflix.loadbalancer.Server)1 ServerList (com.netflix.loadbalancer.ServerList)1 URISyntaxException (java.net.URISyntaxException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 PongMessage (javax.websocket.PongMessage)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1