com.fastchar.socket.client.websocket.FastWebSocketClient Maven / Gradle / Ivy
package com.fastchar.socket.client.websocket;
import com.fastchar.socket.exceptions.FastWebSocketException;
import com.fastchar.socket.interfaces.IFastWebSocketClientListener;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.net.URI;
/**
* 连接WebSocket客户端,不建议在main方法测试
*/
public class FastWebSocketClient {
private static final EventLoopGroup EVENT_LOOP_GROUP = new NioEventLoopGroup();
public static void closeAll() {
EVENT_LOOP_GROUP.shutdownGracefully();
}
public Channel newWebSocket(final String url, final IFastWebSocketClientListener iFastWebSocketClientListener) {
try {
final URI uri = new URI(url);
String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
final int port;
if (uri.getPort() == -1) {
if ("ws".equalsIgnoreCase(scheme)) {
port = 80;
} else if ("wss".equalsIgnoreCase(scheme)) {
port = 443;
} else {
port = -1;
}
} else {
port = uri.getPort();
}
if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
throw new FastWebSocketException("WebSocket地址无效!");
}
boolean ssl = "wss".equalsIgnoreCase(scheme);
final SslContext sslContext;
if (ssl) {
sslContext = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
} else {
sslContext = null;
}
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(EVENT_LOOP_GROUP)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(ch.alloc(), host, port));
}
pipeline.addLast(
new LoggingHandler(LogLevel.TRACE),
new HttpClientCodec(),
new HttpObjectAggregator(8192),
new WebSocketClientCompressionHandler(),
new FastWebSocketConvertHeaderHandler(),
new FastWebSocketClientChannelHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()), iFastWebSocketClientListener, url));
}
});
return bootstrap.connect(host, port).sync().channel();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}