spring boot netty 即时通讯系统之客户端与服务端聊天(八)

通过客户端发送信息给服务端进行消息的推送,推荐从前面的章节开始看。

ChatReqHandler.java

import com.fyrt.fyrtim.util.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Scanner;

@Slf4j
public class ChatReqHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        NettyMessage nettyMessage = (NettyMessage) JsonUtil.parseNettyMessage(msg);
        System.out.println("数据接收到了");
        User user = nettyMessage.getUser();
        if (user != null) {
            System.out.println(user.getUserId() + ":" + nettyMessage.getMsg());
        } else {
            System.out.println(nettyMessage.getMsg());

        }

    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("IM系统准备就绪,请发送消息: ");
        new Thread(() -> {
            while (true) {
                //读取用户在命令行输入的各种数据类型 Terminal控制台输入数据
                Scanner sc = new Scanner(System.in);
                //此扫描器执行当前行,并返回跳过的输入信息
                String line = sc.nextLine();
                HashSet set = new HashSet<>();
                set.add("闪电侠");
                NettyMessage nettyMessage = new NettyMessage(TransportType.STRING.getValue(), line, set, ctx.channel(), UserChannelUtil.user());
                String body = JsonUtil.objectToJson(nettyMessage);
                //发送数据并刷新
                ctx.writeAndFlush(body);
            }
        }).start();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

 

 

ChatRespHandler.java

import com.fyrt.fyrtim.util.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Iterator;
import java.util.Set;

public class ChatRespHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        NettyMessage nettyMessage = (NettyMessage) JsonUtil.parseNettyMessage(msg);
        //用户组
        Set userIds = nettyMessage.getOtherUsers();
        if (userIds.size() > 0) {
            userIds.forEach(userId -> {
                  //判断当前用户是否登录,当前channel中是否存在用户信息
                if (UserChannelUtil.userIdChannelMap.get(userId) == null) {
                    System.out.println("用户不在线");
                } else {
                    Channel channel = UserChannelUtil.getChannel(userId);
                    //获取用户通道
                    User user = UserChannelUtil.getUser(channel);
                    //发送到指定的用户通道
                    channel.writeAndFlush(JsonUtil.objectToJson(new NettyMessage(TransportType.STRING.getValue(), nettyMessage.getMsg(), user)));

                }
            });
        } else {
            ctx.writeAndFlush(JsonUtil.objectToJson(new NettyMessage(TransportType.STRING.getValue(), nettyMessage.getMsg(), ctx.channel())));
        }
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }


}

实现效果:

 

代码已上传至github:https://github.com/beibeirenzhe/netty-im/tree/master/fyrtim

你可能感兴趣的:(netty)