提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
由于使用Java自带的Nio编写websocket的java客户端时,使用的是java_websocket_client,然后在openjdk下出现cpu暴增问题,因此使用netty实现websocket的java客户端,并集成到Spring中
提示:以下是本篇文章正文内容,下面案例可供参考
一、实现websocet连接
package com.wsjkk.netty.websocket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
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.*;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
/**
* This is an example of a WebSocket client.
* <p>
* In order to run this example you need a compatible WebSocket server.
* Therefore you can either start the WebSocket server from the examples
* by running {@link io.netty.example.http.websocketx.server}
* or connect to an existing WebSocket server such as
* <a href="htt