刚接触netty,就被他深深吸引.经过4天的不断学习和研究,终于被我跑通了,以下我就分享给大家!
首先我吐槽以下他官方对protobuf多协议那种堪比shit的支持,导致我4天3天都在坑里摸爬滚打!
以下是我自创的,支持多协议,又不失优雅的写法,目前网上找不到的! 不多说,走你
首先是服务端的启动:
package com.zhcj.netty;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import io.netty.bootstrap.ServerBootstrap;
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.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
/**
*
* @author Dan
* 2020-07-04
*
*/
@Service
public class NettyServer {
public static void main(String[] args) {
new NettyServer().run();
}
@PostConstruct
public void initNetty() {
new Thread() {
public void run() {
new NettyServer().run();
}
}.start();
}
public void run() {
System.out.println("===========================Netty端口启动===========&