• 大小: 1.43MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-25
  • 语言: Java
  • 标签: netty  

资源简介

- 本实例是《Netty 粘包/半包原理与拆包实战》 一文的源代码工程。 大家好,我是作者尼恩。 在前面的文章中,完成了一个高性能的 Java 聊天程序,尼恩已经再一次的进行了通讯协议的选择。放弃了大家非常熟悉的json 格式,选择了性能更佳的 Protobuf协议。 本实例,解决粘包和半包问题

资源截图

代码片段和文件信息

package com.crazymakercircle.chat.client;

import com.crazymakercircle.chat.common.ClientMsgBuilder;
import com.crazymakercircle.chat.common.bean.ChatMsg;
import com.crazymakercircle.chat.common.bean.User;
import com.crazymakercircle.chat.common.bean.msg.ProtoMsg;
import com.crazymakercircle.chat.common.codec.PackageSpliter;
import com.crazymakercircle.chat.common.codec.ProtobufDecoder;
import com.crazymakercircle.chat.common.codec.ProtobufEncoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service(“EchoClient“)
public class ChatClient
{
    static final Logger LOGGER =
            LoggerFactory.getLogger(ChatClient.class);
    // 服务器ip地址
    @Value(“${server.ip}“)
    private String host;
    // 服务器端口
    @Value(“${server.port}“)
    private int port;

    // 通过nio方式来接收连接和处理连接
    private EventLoopGroup group = new NioEventLoopGroup();

    @Autowired
    private ChatClientHandler chatClientHandler;

    private Channel channel;
    private User user;

    /**
     * 唯一标记
     */
    private boolean initFalg = true;

    /**
     * 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
     * 都是AbstractBootstrap的子类。
     **/
    public void run()
    {
        doConnect(new Bootstrap() group);
    }

    /**
     * 重连
     */
    public void doConnect(Bootstrap bootstrap EventLoopGroup eventLoopGroup)
    {
        ChannelFuture f = null;
        try
        {
            if (bootstrap != null)
            {
                bootstrap.group(eventLoopGroup);
                bootstrap.channel(NioSocketChannel.class);
                bootstrap.option(ChannelOption.SO_KEEPALIVE true);
                bootstrap.option(ChannelOption.ALLOCATOR PooledByteBufAllocator.DEFAULT);
                bootstrap.remoteAddress(host port);

                // 设置通道初始化
                bootstrap.handler(
                        new ChannelInitializer()
                        {
                            public void initChannel(SocketChannel ch) throws Exception
                            {
                                ch.pipeline().addLast(new PackageSpliter());
                                ch.pipeline().addLast(new ProtobufDecoder());
                                ch.pipeline().addLast(new ProtobufEncoder());
                                ch.pipeline().addLast(chatClientHandler);

                            }
                        }
                );

             

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-11 10:11  .idea\
     文件         849  2018-11-09 10:26  .idea\compiler.xml
     文件         373  2018-11-09 10:26  .idea\encodings.xml
     文件         232  2018-11-07 20:32  .idea\kotlinc.xml
     目录           0  2018-11-09 15:17  .idea\libraries\
     文件         575  2018-11-07 20:24  .idea\libraries\Maven__ch_qos_logback_logback_classic_1_1_11.xml
     文件         554  2018-11-07 20:24  .idea\libraries\Maven__ch_qos_logback_logback_core_1_1_11.xml
     文件         574  2018-11-09 13:48  .idea\libraries\Maven__com_google_protobuf_protobuf_java_2_6_1.xml
     文件         546  2018-11-07 20:24  .idea\libraries\Maven__com_jayway_jsonpath_json_path_2_2_0.xml
     文件         700  2018-11-07 20:24  .idea\libraries\Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml
     文件         525  2018-11-09 15:17  .idea\libraries\Maven__commons_lang_commons_lang_2_6.xml
     文件         551  2018-11-07 20:26  .idea\libraries\Maven__io_netty_netty_all_4_0_33_Final.xml
     文件         455  2018-11-07 20:24  .idea\libraries\Maven__junit_junit_4_12.xml
     文件         542  2018-11-07 20:24  .idea\libraries\Maven__net_minidev_accessors_smart_1_1.xml
     文件         521  2018-11-07 20:24  .idea\libraries\Maven__net_minidev_json_smart_2_2_1.xml
     文件         535  2018-11-07 20:24  .idea\libraries\Maven__org_assertj_assertj_core_2_6_0.xml
     文件         532  2018-11-07 20:24  .idea\libraries\Maven__org_hamcrest_hamcrest_core_1_3.xml
     文件         553  2018-11-07 20:24  .idea\libraries\Maven__org_hamcrest_hamcrest_library_1_3.xml
     文件         549  2018-11-07 20:24  .idea\libraries\Maven__org_mockito_mockito_core_1_10_19.xml
     文件         508  2018-11-07 20:24  .idea\libraries\Maven__org_objenesis_objenesis_2_1.xml
     文件         472  2018-11-07 20:24  .idea\libraries\Maven__org_ow2_asm_asm_5_0_3.xml
     文件         537  2018-11-07 20:24  .idea\libraries\Maven__org_skyscreamer_jsonassert_1_4_0.xml
     文件         548  2018-11-07 20:24  .idea\libraries\Maven__org_slf4j_jcl_over_slf4j_1_7_25.xml
     文件         534  2018-11-07 20:24  .idea\libraries\Maven__org_slf4j_jul_to_slf4j_1_7_25.xml
     文件         562  2018-11-07 20:24  .idea\libraries\Maven__org_slf4j_log4j_over_slf4j_1_7_25.xml
     文件         513  2018-11-07 20:24  .idea\libraries\Maven__org_slf4j_slf4j_api_1_7_16.xml
     文件         636  2018-11-07 20:24  .idea\libraries\Maven__org_springframework_boot_spring_boot_1_5_9_RELEASE.xml
     文件         734  2018-11-07 20:24  .idea\libraries\Maven__org_springframework_boot_spring_boot_autoconfigure_1_5_9_RELEASE.xml
     文件         699  2018-11-07 20:24  .idea\libraries\Maven__org_springframework_boot_spring_boot_devtools_1_5_9_RELEASE.xml
     文件         692  2018-11-07 20:24  .idea\libraries\Maven__org_springframework_boot_spring_boot_starter_1_5_9_RELEASE.xml
     文件         748  2018-11-07 20:24  .idea\libraries\Maven__org_springframework_boot_spring_boot_starter_logging_1_5_9_RELEASE.xml
............此处省略212个文件信息

评论

共有 条评论