Search in sources :

Example 1 with WebSocketSession

use of org.springframework.web.reactive.socket.WebSocketSession 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 2 with WebSocketSession

use of org.springframework.web.reactive.socket.WebSocketSession 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 3 with WebSocketSession

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

the class ReactorNettyWebSocketClient method execute.

@Override
public Mono<Void> execute(URI url, HttpHeaders headers, WebSocketHandler handler) {
    String[] protocols = beforeHandshake(url, headers, handler);
    return getHttpClient().ws(url.toString(), nettyHeaders -> setNettyHeaders(headers, nettyHeaders), StringUtils.arrayToCommaDelimitedString(protocols)).then(response -> {
        HandshakeInfo info = afterHandshake(url, toHttpHeaders(response));
        ByteBufAllocator allocator = response.channel().alloc();
        NettyDataBufferFactory factory = new NettyDataBufferFactory(allocator);
        return response.receiveWebsocket((in, out) -> {
            WebSocketSession session = new ReactorNettyWebSocketSession(in, out, info, factory);
            return handler.handle(session);
        });
    });
}
Also used : HttpClientOptions(reactor.ipc.netty.http.client.HttpClientOptions) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Consumer(java.util.function.Consumer) HttpClient(reactor.ipc.netty.http.client.HttpClient) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) StringUtils(org.springframework.util.StringUtils) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)

Aggregations

WebSocketSession (org.springframework.web.reactive.socket.WebSocketSession)3 ByteBuffer (java.nio.ByteBuffer)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 WebSocketMessage (org.springframework.web.reactive.socket.WebSocketMessage)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 URI (java.net.URI)1 Consumer (java.util.function.Consumer)1 PongMessage (javax.websocket.PongMessage)1 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)1 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 StringUtils (org.springframework.util.StringUtils)1 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)1 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)1 ReactorNettyWebSocketSession (org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)1 Mono (reactor.core.publisher.Mono)1 HttpClient (reactor.ipc.netty.http.client.HttpClient)1 HttpClientOptions (reactor.ipc.netty.http.client.HttpClientOptions)1 HttpClientResponse (reactor.ipc.netty.http.client.HttpClientResponse)1