/** * WebSocket to the passed URL. * * @param url the target remote URL * @param headerBuilder the header {@link Consumer} to invoke before sending websocket * handshake * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> ws(String url, final Consumer<? super HttpHeaders> headerBuilder) { return request(WS, url, ch -> { headerBuilder.accept(ch.requestHeaders()); return ch.sendWebsocket(); }); } /**
@Override default HttpClientRequest context(Consumer<NettyContext> contextCallback){ contextCallback.accept(context()); return this; }
static Function<? super HttpClientRequest, ? extends Publisher<Void>> handler(Function<? super HttpClientRequest, ? extends Publisher<Void>> h, HttpClientOptions opts) { if (opts.acceptGzip()) { if (h != null) { return req -> h.apply(req.header(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP)); } else { return req -> req.header(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); } } else { return h; } }
/** * WebSocket to the passed URL, negotiating one of the passed subprotocols. * <p> * The negotiated subprotocol can be accessed through the {@link HttpClientResponse} * by switching to websocket (using any of the {@link HttpClientResponse#receiveWebsocket() receiveWebSocket} * methods) and using {@link WebsocketInbound#selectedSubprotocol()}. * <p> * To send data through the websocket, use {@link HttpClientResponse#receiveWebsocket(BiFunction)} * and then use the function's {@link WebsocketOutbound}. * * @param url the target remote URL * @param subprotocols the subprotocol(s) to negotiate, comma-separated, or null if not relevant. * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> ws(String url, String subprotocols) { return request(WS, url, req -> req.sendWebsocket(subprotocols)); }
private HttpClientRequest addHeaders(HttpClientRequest request, HttpHeaders headers) { headers.keySet().stream().forEach(key -> request.requestHeaders().set(key, headers.get(key))); return request; }
zipkinSpansUrl, request -> { request.addHeader("Content-Type", "application/json"); return request.sendString(Mono.just(spans)); }) .timeout(Duration.ofSeconds(30))
/** * Send headers and empty content thus delimiting a full empty body http request * * @return a {@link Mono} successful on committed response * * @see #send(Publisher) */ default Mono<Void> send() { return sendObject(Unpooled.EMPTY_BUFFER).then(); }
@Override public Mono<Void> execute(URI url, HttpHeaders headers, WebSocketHandler handler) { String[] protocols = beforeHandshake(url, headers, handler); // TODO: https://github.com/reactor/reactor-netty/issues/20 return getHttpClient() .get(url.toString(), request -> addHeaders(request, headers).sendWebsocket()) .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); }); }); }
/** * WebSocket to the passed URL, negotiating one of the passed subprotocols. * <p> * The negotiated subprotocol can be accessed through the {@link HttpClientResponse} * by switching to websocket (using any of the {@link HttpClientResponse#receiveWebsocket() receiveWebSocket} * methods) and using {@link WebsocketInbound#selectedSubprotocol()}. * <p> * To send data through the websocket, use {@link HttpClientResponse#receiveWebsocket(BiFunction)} * and then use the function's {@link WebsocketOutbound}. * * @param url the target remote URL * @param headerBuilder the header {@link Consumer} to invoke before sending websocket * handshake * @param subprotocols the subprotocol(s) to negotiate, comma-separated, or null if not relevant. * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> ws(String url, final Consumer<? super HttpHeaders> headerBuilder, String subprotocols) { return request(WS, url, ch -> { headerBuilder.accept(ch.requestHeaders()); return ch.sendWebsocket(subprotocols); }); }