use of io.micronaut.http.client.netty.websocket.NettyWebSocketClientHandler in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method connectWebSocket.
private <T> Flux<T> connectWebSocket(URI uri, MutableHttpRequest<?> request, Class<T> clientEndpointType, WebSocketBean<T> webSocketBean) {
Bootstrap bootstrap = this.bootstrap.clone();
if (webSocketBean == null) {
webSocketBean = webSocketRegistry.getWebSocket(clientEndpointType);
}
WebSocketBean<T> finalWebSocketBean = webSocketBean;
return Flux.create(emitter -> {
SslContext sslContext = buildSslContext(uri);
WebSocketVersion protocolVersion = finalWebSocketBean.getBeanDefinition().enumValue(ClientWebSocket.class, "version", WebSocketVersion.class).orElse(WebSocketVersion.V13);
int maxFramePayloadLength = finalWebSocketBean.messageMethod().map(m -> m.intValue(OnMessage.class, "maxPayloadLength").orElse(65536)).orElse(65536);
String subprotocol = finalWebSocketBean.getBeanDefinition().stringValue(ClientWebSocket.class, "subprotocol").orElse(StringUtils.EMPTY_STRING);
RequestKey requestKey;
try {
requestKey = new RequestKey(uri);
} catch (HttpClientException e) {
emitter.error(e);
return;
}
bootstrap.remoteAddress(requestKey.getHost(), requestKey.getPort());
initBootstrapForProxy(bootstrap, sslContext != null, requestKey.getHost(), requestKey.getPort());
bootstrap.handler(new HttpClientInitializer(sslContext, requestKey.getHost(), requestKey.getPort(), false, false, false, null) {
@Override
protected void addFinalHandler(ChannelPipeline pipeline) {
pipeline.remove(ChannelPipelineCustomizer.HANDLER_HTTP_DECODER);
ReadTimeoutHandler readTimeoutHandler = pipeline.get(ReadTimeoutHandler.class);
if (readTimeoutHandler != null) {
pipeline.remove(readTimeoutHandler);
}
Optional<Duration> readIdleTime = configuration.getReadIdleTimeout();
if (readIdleTime.isPresent()) {
Duration duration = readIdleTime.get();
if (!duration.isNegative()) {
pipeline.addLast(ChannelPipelineCustomizer.HANDLER_IDLE_STATE, new IdleStateHandler(duration.toMillis(), duration.toMillis(), duration.toMillis(), TimeUnit.MILLISECONDS));
}
}
final NettyWebSocketClientHandler webSocketHandler;
try {
String scheme = (sslContext == null) ? "ws" : "wss";
URI webSocketURL = UriBuilder.of(uri).scheme(scheme).host(host).port(port).build();
MutableHttpHeaders headers = request.getHeaders();
HttpHeaders customHeaders = EmptyHttpHeaders.INSTANCE;
if (headers instanceof NettyHttpHeaders) {
customHeaders = ((NettyHttpHeaders) headers).getNettyHeaders();
}
if (StringUtils.isNotEmpty(subprotocol)) {
customHeaders.add("Sec-WebSocket-Protocol", subprotocol);
}
webSocketHandler = new NettyWebSocketClientHandler<>(request, finalWebSocketBean, WebSocketClientHandshakerFactory.newHandshaker(webSocketURL, protocolVersion, subprotocol, true, customHeaders, maxFramePayloadLength), requestBinderRegistry, mediaTypeCodecRegistry, emitter);
pipeline.addLast(WebSocketClientCompressionHandler.INSTANCE);
pipeline.addLast(ChannelPipelineCustomizer.HANDLER_MICRONAUT_WEBSOCKET_CLIENT, webSocketHandler);
} catch (Throwable e) {
emitter.error(new WebSocketSessionException("Error opening WebSocket client session: " + e.getMessage(), e));
}
}
});
addInstrumentedListener(bootstrap.connect(), future -> {
if (!future.isSuccess()) {
emitter.error(future.cause());
}
});
}, FluxSink.OverflowStrategy.ERROR);
}
Aggregations