|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.reactive.socket.adapter; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | + |
| 20 | +import io.netty.handler.codec.http.websocketx.WebSocketFrame; |
| 21 | +import org.reactivestreams.Publisher; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | +import reactor.ipc.netty.http.HttpInbound; |
| 25 | +import reactor.ipc.netty.http.HttpOutbound; |
| 26 | + |
| 27 | +import org.springframework.core.io.buffer.NettyDataBufferFactory; |
| 28 | +import org.springframework.web.reactive.socket.CloseStatus; |
| 29 | +import org.springframework.web.reactive.socket.WebSocketMessage; |
| 30 | +import org.springframework.web.reactive.socket.WebSocketSession; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Spring {@link WebSocketSession} adapter for RxNetty's |
| 35 | + * {@link io.reactivex.netty.protocol.http.ws.WebSocketConnection}. |
| 36 | + * |
| 37 | + * @author Rossen Stoyanchev |
| 38 | + * @since 5.0 |
| 39 | + */ |
| 40 | +public class ReactorNettyWebSocketSession |
| 41 | + extends NettyWebSocketSessionSupport<ReactorNettyWebSocketSession.WebSocketConnection> { |
| 42 | + |
| 43 | + |
| 44 | + protected ReactorNettyWebSocketSession(HttpInbound inbound, HttpOutbound outbound, |
| 45 | + URI uri, NettyDataBufferFactory factory) { |
| 46 | + |
| 47 | + super(new WebSocketConnection(inbound, outbound), uri, factory); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @Override |
| 52 | + public Flux<WebSocketMessage> receive() { |
| 53 | + HttpInbound inbound = getDelegate().getHttpInbound(); |
| 54 | + return toMessageFlux(inbound.receiveObject().cast(WebSocketFrame.class)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Mono<Void> send(Publisher<WebSocketMessage> messages) { |
| 59 | + HttpOutbound outbound = getDelegate().getHttpOutbound(); |
| 60 | + Flux<WebSocketFrame> frameFlux = Flux.from(messages).map(this::toFrame); |
| 61 | + return outbound.sendObject(frameFlux); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected Mono<Void> closeInternal(CloseStatus status) { |
| 66 | + return Mono.error(new UnsupportedOperationException( |
| 67 | + "Currently in Reactor Netty applications are expected to use the Cancellation" + |
| 68 | + "returned from subscribing to the input Flux to close the WebSocket session.")); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Simple container for {@link HttpInbound} and {@link HttpOutbound}. |
| 74 | + */ |
| 75 | + public static class WebSocketConnection { |
| 76 | + |
| 77 | + private final HttpInbound inbound; |
| 78 | + |
| 79 | + private final HttpOutbound outbound; |
| 80 | + |
| 81 | + |
| 82 | + public WebSocketConnection(HttpInbound inbound, HttpOutbound outbound) { |
| 83 | + this.inbound = inbound; |
| 84 | + this.outbound = outbound; |
| 85 | + } |
| 86 | + |
| 87 | + public HttpInbound getHttpInbound() { |
| 88 | + return this.inbound; |
| 89 | + } |
| 90 | + |
| 91 | + public HttpOutbound getHttpOutbound() { |
| 92 | + return this.outbound; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments