• 大小: 1.56MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-04
  • 语言: 其他
  • 标签: netty  

资源简介

来自于疯狂创客圈 《netty+protobuf 整合实战》的源代码,付上了 protobuf 的 protoc 工具, protoc-2.6.1-win32.zip

资源截图

代码片段和文件信息

package com.crazymakercircle.chat.client;

import com.crazymakercircle.chat.common.ClientMsgBuilder;
import com.crazymakercircle.chat.common.Session;
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.ProtobufDecoder;
import com.crazymakercircle.chat.common.codec.ProtobufEncoder;
import com.crazymakercircle.util;
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 io.netty.util.AttributeKey;
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.Scanner;
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 ProtobufDecoder());
                                ch.pipeline().addLast(new ProtobufEncoder());
                                ch.pipeline().addLast(chatClientHandler);

                            }
                        }
                );

      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-09 22:10  .git\
     文件           7  2018-11-07 23:36  .git\COMMIT_EDITMSG
     文件          23  2018-11-07 20:14  .git\HEAD
     文件         334  2018-11-09 13:58  .git\config
     文件          73  2018-11-07 20:14  .git\description
     目录           0  2018-11-07 20:14  .git\hooks\
     文件         478  2018-11-07 20:14  .git\hooks\applypatch-msg.sample
     文件         896  2018-11-07 20:14  .git\hooks\commit-msg.sample
     文件        3327  2018-11-07 20:14  .git\hooks\fsmonitor-watchman.sample
     文件         189  2018-11-07 20:14  .git\hooks\post-update.sample
     文件         424  2018-11-07 20:14  .git\hooks\pre-applypatch.sample
     文件        1638  2018-11-07 20:14  .git\hooks\pre-commit.sample
     文件        1348  2018-11-07 20:14  .git\hooks\pre-push.sample
     文件        4898  2018-11-07 20:14  .git\hooks\pre-rebase.sample
     文件         544  2018-11-07 20:14  .git\hooks\pre-receive.sample
     文件        1492  2018-11-07 20:14  .git\hooks\prepare-commit-msg.sample
     文件        3610  2018-11-07 20:14  .git\hooks\update.sample
     文件        3688  2018-11-09 19:50  .git\index
     目录           0  2018-11-07 20:14  .git\info\
     文件         240  2018-11-07 20:14  .git\info\exclude
     目录           0  2018-11-07 21:14  .git\logs\
     文件         452  2018-11-07 23:36  .git\logs\HEAD
     目录           0  2018-11-07 21:59  .git\logs\refs\
     目录           0  2018-11-07 21:14  .git\logs\refs\heads\
     文件         452  2018-11-07 23:36  .git\logs\refs\heads\master
     目录           0  2018-11-07 21:59  .git\logs\refs\remotes\
     目录           0  2018-11-07 21:59  .git\logs\refs\remotes\origin\
     文件         280  2018-11-07 23:37  .git\logs\refs\remotes\origin\master
     目录           0  2018-11-09 19:50  .git\objects\
     目录           0  2018-11-09 19:06  .git\objects\00\
     文件         333  2018-11-09 19:19  .git\objects\00\171e92085186cd48e7309af953de24fe0ccc4e
............此处省略462个文件信息

评论

共有 条评论