final Mono<Void> withWebsocketSupport(URI url,
String protocols,
BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> websocketHandler) {
if (markSentHeaders()) {
addHandlerFirst(NettyPipeline.HttpAggregator, new HttpObjectAggregator(8192));
HttpClientWSOperations ops = new HttpClientWSOperations(url, protocols, this);
if (replace(ops)) {
Mono<Void> handshake = FutureMono.from(ops.handshakerResult);
return applyWebsocketHandler(ops, handshake, websocketHandler);
}
}
else if (isWebsocket()) {
HttpClientWSOperations ops = (HttpClientWSOperations) get(channel());
if(ops != null) {
Mono<Void> handshake = FutureMono.from(ops.handshakerResult);
return applyWebsocketHandler(ops, handshake, websocketHandler);
}
}
else {
log.error(format(channel(), "Cannot enable websocket if headers have already been sent"));
}
return Mono.error(new IllegalStateException("Failed to upgrade to websocket"));
}