/** * Accept a request from a client to establish duplex communications as a server. * * @param request request sent by the client. * <p> * The request body is assumed to consist of an * infinite stream of chunks, each representing a message from the client. * @param createHandler callback that takes a message sender that can be used to send out messages, and returns a * message handler receives messages from the remote. * @return response to be sent to the client to initiate duplex communication. */ public RawHttpResponse<Void> accept(RawHttpRequest request, Function<MessageSender, MessageHandler> createHandler) { final Iterator<Chunk> receivedChunkStream = request.getBody() .flatMap((b) -> { try { return b.asChunkStream(); } catch (IOException e) { throw new RuntimeException(e); } }) .orElseThrow(() -> new IllegalStateException("Client did not send chunked body")); return accept(receivedChunkStream, createHandler); }