美文网首页
4.长连接之心跳检测

4.长连接之心跳检测

作者: 八颗小牙坏脾气 | 来源:发表于2018-12-14 22:54 被阅读0次

Server

public class Server {
    public static final int INET_PORT = 8888;
    public static void main(String[] args) throws Exception {
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            serverBootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ServerChannelInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(INET_PORT).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new IdleStateHandler(5, 10, 8, TimeUnit.SECONDS));
        pipeline.addLast(new ServerChannelHandler());
    }
}

public class ServerChannelHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventMsg = StringUtil.EMPTY_STRING;
            switch (event.state()) {
                case READER_IDLE:
                    eventMsg = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventMsg = "写空闲";
                    break;
                case ALL_IDLE:
                    eventMsg = "读写空闲";
                    break;
            }

            System.out.println(ctx.channel().remoteAddress() + "," + eventMsg);
        }
    }
}

Client

public class MyChatClient {
    public static void main(String[] args) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new MyChatClientInitializer());
            Channel channel = bootstrap.connect("localhost", 8888).sync().channel();

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            for (; ; ) {
                channel.writeAndFlush(br.readLine() + "\r\n");
            }

        } finally {
            group.shutdownGracefully();
        }
    }
}
public class MyChatClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
        pipeline.addLast(new MyChatClientlHandler());
    }
}
public class MyChatClientlHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }
}

相关文章

  • 4.长连接之心跳检测

    Server Client

  • netty心跳检测机制IdleStateHandler分析

    netty中使用了IdleStateHandler来进行心跳检测,客户端和服务端保持长连接需要通过一个检测机制来确...

  • Netty心跳基本检测机制

    Netty心跳基本检测机制 首先,了解下为什么需要心跳?假设客户端(如手机,PAD)与服务器端已经建立了长连接,客...

  • 长连接和心跳

    长连接实现的几种方式 socket建立长连接并发送心跳

  • Android实现websocket长连接

    参考:(61条消息) android接入简单的websocket步骤,建立一个长连接(带心跳检测)从服务器端接收消...

  • Java实现web心跳的长连接检测机制

    netty实现 参考:https://github.com/zhuchen-heavy/nebula-netty-...

  • Dubbo 长连接实现与配置

    初始连接:引用服务|增加提供者==>获取连接===》是否获取共享连接==>创建连接客户端==》开启心跳检测状态检查...

  • tcp长连接、心跳包

    长连接 TCP经过三次握手建立连接,长连接是指不管有无数据包的发送都长期保持建立的连接;有长连接自然也有短连接,短...

  • 心跳

    什么是心跳? 心跳就是用来检测TCP连接的双方是否可用。但是TCP本身不是有就一个keepAlive机制么,那是因...

  • Netty - Netty 重连实现

    学习完整课程请移步 互联网 Java 全栈工程师 心跳机制检测连接存活 启动时连接重试 运行中连接断开时重试

网友评论

      本文标题:4.长连接之心跳检测

      本文链接:https://www.haomeiwen.com/subject/hngnhqtx.html