-
Notifications
You must be signed in to change notification settings - Fork 0
websocket client
package cn.org.bjca.tt;
import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; 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.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler; import io.netty.handler.codec.http.websocketx.WebSocketVersion; import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler; import org.apache.commons.lang.RandomStringUtils; import org.dom4j.Document; import org.dom4j.io.SAXReader; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.nio.charset.Charset; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream;
/**
- @Author lijiansong@bjca.org.cn
- @Date: 2019/10/29 */
public class Main {
public static void main(String[] args) throws Exception{
Bootstrap bootstrap = new Bootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
URI uri = new URI("ws://127.0.0.1:8090/echo");
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri,WebSocketVersion.V13,null, true, new DefaultHttpHeaders()) ;
WebSocketClientProtocolHandler protocolHandler = new WebSocketClientProtocolHandler(handshaker);
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline channelPipeline = channel.pipeline();
channelPipeline.addLast(new HttpClientCodec());
channelPipeline.addLast(new HttpObjectAggregator(8192));
channelPipeline.addLast(WebSocketClientCompressionHandler.INSTANCE);
channelPipeline.addLast(protocolHandler);
channelPipeline.addLast(new ClientHandler());
}
});
ChannelFuture future = bootstrap.connect(uri.getHost(),uri.getPort()).sync();
future.channel().closeFuture().sync();
}
private static class ClientHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if(evt instanceof WebSocketClientProtocolHandler.ClientHandshakeStateEvent){
WebSocketClientProtocolHandler.ClientHandshakeStateEvent event = (WebSocketClientProtocolHandler.ClientHandshakeStateEvent) evt;
if(event == WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE){
System.out.println("握手成功");
}
}
}
}
}