下面列出了 io.netty.handler.codec.http.websocketx.WebSocketVersion # V13 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates an instance of {@link WebSocketClientHandler} with {@link AwsSigV4ClientHandshaker} as the handshaker
* for SigV4 auth.
* @return the instance of clientHandler.
*/
private WebSocketClientHandler createHandler() {
HandshakeRequestConfig handshakeRequestConfig =
HandshakeRequestConfig.parse(cluster.authProperties().get(AuthProperties.Property.JAAS_ENTRY));
WebSocketClientHandshaker handshaker = new LBAwareAwsSigV4ClientHandshaker(
connection.getUri(),
WebSocketVersion.V13,
null,
false,
EmptyHttpHeaders.INSTANCE,
cluster.getMaxContentLength(),
new ChainedSigV4PropertiesProvider(),
handshakeRequestConfig);
return new WebSocketClientHandler(handshaker);
}
static WebSocketVersion getWsVersion(String str) {
switch (str) {
case "0":
return WebSocketVersion.V00;
case "7":
return WebSocketVersion.V07;
case "8":
return WebSocketVersion.V08;
case "13":
return WebSocketVersion.V13;
}
return WebSocketVersion.UNKNOWN;
}
@Override
public void addToPipeline(final ChannelPipeline pipeline) {
pipeline.addLast("http-codec", new HttpClientCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
final WebSocketClientHandshaker handShaker = new WhiteSpaceInPathWebSocketClientHandshaker13(serverUri,
WebSocketVersion.V13, PROTOCOL, false, createHttpHeaders(httpHeaders), Integer.MAX_VALUE);
pipeline.addLast("websocket-protocol-handler", new WebSocketClientProtocolHandler(handShaker));
pipeline.addLast("websocket-frame-codec", new ByteBufToWebSocketFrameCodec());
}
public <R> CompletableFuture<R> connect(Function<Channel, R> connectFunction) {
io.netty.bootstrap.Bootstrap b = new io.netty.bootstrap.Bootstrap();
int actualPort = uri.getPort() == -1 ? (uri.getScheme().equals("wss") ? 443 : 80) : uri.getPort();
final WebSocketClientHandler handler =
new WebSocketClientHandler(
new WebSocketClientHandshaker13(
uri, WebSocketVersion.V13, null, false, HttpHeaders.EMPTY_HEADERS, 1280000) {
@Override
protected FullHttpRequest newHandshakeRequest() {
FullHttpRequest request = super.newHandshakeRequest();
if (clientNegotiation.getSupportedSubProtocols() != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < clientNegotiation.getSupportedSubProtocols().size(); ++i) {
if (i > 0) {
sb.append(", ");
}
sb.append(clientNegotiation.getSupportedSubProtocols().get(i));
}
request.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, sb.toString());
}
clientNegotiation.beforeRequest(request.headers());
return request;
}
@Override
protected void verify(FullHttpResponse response) {
super.verify(response);
clientNegotiation.afterRequest(response.headers());
}
}, connectFunction);
b.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (ssl != null) {
SSLEngine sslEngine = ssl.createSSLEngine(uri.getHost(), actualPort);
sslEngine.setUseClientMode(true);
pipeline.addLast("ssl", new SslHandler(sslEngine));
}
pipeline.addLast("http-codec", new HttpClientCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
pipeline.addLast("ws-handler", handler);
}
});
//System.out.println("WebSocket Client connecting");
b.connect(uri.getHost(), actualPort).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.cause() != null) {
handler.handshakeFuture.completeExceptionally(future.cause());
}
}
});
return handler.handshakeFuture;
}
public WebsocketClientHandshaker(URI webSocketURL, HttpHeaders customHeaders, int maxFramePayloadLength) {
super(webSocketURL, WebSocketVersion.V13, null, false, customHeaders, maxFramePayloadLength);
}