Search in sources :

Example 6 with WebSocketMessage

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

the class JettyWebSocketHandlerAdapter method onWebSocketBinary.

@OnWebSocketMessage
public void onWebSocketBinary(byte[] message, int offset, int length) {
    if (this.delegateSession != null) {
        ByteBuffer buffer = ByteBuffer.wrap(message, offset, length);
        WebSocketMessage webSocketMessage = toMessage(Type.BINARY, buffer);
        this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
    }
}
Also used : OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) ByteBuffer(java.nio.ByteBuffer) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 7 with WebSocketMessage

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

the class JettyWebSocketHandlerAdapter method toMessage.

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

Example 8 with WebSocketMessage

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

the class StandardWebSocketHandlerAdapter method onOpen.

@Override
public void onOpen(Session session, EndpointConfig config) {
    this.delegateSession = this.sessionFactory.apply(session);
    session.addMessageHandler(String.class, message -> {
        WebSocketMessage webSocketMessage = toMessage(message);
        this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
    });
    session.addMessageHandler(ByteBuffer.class, message -> {
        WebSocketMessage webSocketMessage = toMessage(message);
        this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
    });
    session.addMessageHandler(PongMessage.class, message -> {
        WebSocketMessage webSocketMessage = toMessage(message);
        this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
    });
    this.delegateHandler.handle(this.delegateSession).subscribe(this.delegateSession);
}
Also used : WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage)

Example 9 with WebSocketMessage

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

the class WebSocketIntegrationTests method customHeader.

@Test
public void customHeader() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.add("my-header", "my-value");
    MonoProcessor<Object> output = MonoProcessor.create();
    client.execute(getUrl("/custom-header"), headers, session -> session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then()).block(Duration.ofMillis(5000));
    assertEquals("my-header:my-value", output.block(Duration.ofMillis(5000)));
}
Also used : Arrays(java.util.Arrays) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) WebSocketHandlerAdapter(org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter) RibbonClient(org.springframework.cloud.netflix.ribbon.RibbonClient) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) URI(java.net.URI) HandlerMapping(org.springframework.web.reactive.HandlerMapping) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) Server(com.netflix.loadbalancer.Server) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) RouteLocatorBuilder(org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder) HttpHeaders(org.springframework.http.HttpHeaders) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SEC_WEBSOCKET_PROTOCOL(org.springframework.cloud.gateway.filter.WebsocketRoutingFilter.SEC_WEBSOCKET_PROTOCOL) ReactorNettyRequestUpgradeStrategy(org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) HandshakeWebSocketService(org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService) LogFactory(org.apache.commons.logging.LogFactory) ReactorHttpServer(org.springframework.cloud.gateway.test.support.ReactorHttpServer) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) PermitAllSecurityConfiguration(org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration) HashMap(java.util.HashMap) RouteLocator(org.springframework.cloud.gateway.route.RouteLocator) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) StaticServerList(org.springframework.cloud.netflix.ribbon.StaticServerList) Value(org.springframework.beans.factory.annotation.Value) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) Before(org.junit.Before) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) WebSocketService(org.springframework.web.reactive.socket.server.WebSocketService) ReplayProcessor(reactor.core.publisher.ReplayProcessor) Lifecycle(org.springframework.context.Lifecycle) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpServer(org.springframework.cloud.gateway.test.support.HttpServer) Log(org.apache.commons.logging.Log) ServerList(com.netflix.loadbalancer.ServerList) Bean(org.springframework.context.annotation.Bean) Assert.assertEquals(org.junit.Assert.assertEquals) StringUtils(org.springframework.util.StringUtils) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) Test(org.junit.Test)

Example 10 with WebSocketMessage

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

the class JettyWebSocketHandlerAdapter method onWebSocketFrame.

@OnWebSocketFrame
public void onWebSocketFrame(Frame frame) {
    if (this.delegateSession != null) {
        if (OpCode.PONG == frame.getOpCode()) {
            ByteBuffer buffer = (frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD);
            WebSocketMessage webSocketMessage = toMessage(Type.PONG, buffer);
            this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
        }
    }
}
Also used : OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) ByteBuffer(java.nio.ByteBuffer) OnWebSocketFrame(org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)

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