总算是破了我的github处,jmeter压测过了,程序没崩用来显示的Chrome崩了。。。
Github地址:https://github.com/LikeAngPlease/webSocket
具体怎么用的大家看下readme吧,在这里不啰嗦了。讲些有用的
这个注释详细的,我感觉没啥可说的了,不懂的私信问我吧。。。
package org.ang.service;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.ImmediateEventExecutor;
import org.ang.handler.AngIdleStateHandler;
import org.ang.handler.BusinessWebSocketHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import java.util.concurrent.TimeUnit;
/**
* @author Liang
*/
public class NettyServer implements Runnable {
@Value("${readerIdleTime}")
int readerIdleTime;
private final int port;
private static final Logger log = LogManager.getLogger(NettyServer.class);
private final ChannelGroup group = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
public NettyServer(int port) {
this.port = port;
}
public void start() throws Exception {
//创建两个线程组 含有的子线程NioEventLoop的个数默认为CUP核数的二倍 bossGroup 只负责处理请求 workGroup会负责和客户端的业务处理
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
//创建服务端启动对象
ServerBootstrap sb = new ServerBootstrap();
//初始化服务器队列连接大小
sb.option(Chan