Netty系列(三):Netty服务端发送消息到客户端

通常客户端只会主动发送心跳消息,目的是为了保持与服务端连接,而其他消息往往需要服务端发送消息至客户端调取。

实现步骤

  1. 客户端在第一次与服务端建立连接时,将此连接的通道在 Map 中保存下来,为了保证线程安全,可以使用线程安全的 ConcurrentHashMap

  2. 在发送消息给客户端时,通过设备标识遍历 ConcurrentHashMap 找到目标客户端连接通道。找到后先判断通道是否存活,如果连接是存活状态,就通过此通道发送消息给客户端,如果不是存活状态,就从 Map 中删除此通道信息。

  3. 将消息发送至客户端后,服务端正常接收客户端传回的信息。

实现代码

前两篇文章中已经提供了 netty 的整体框架代码,这里只提供一些核心的关键代码,其余代码不再赘述。

指路:

  1. Netty系列(一):Springboot整合Netty,自定义协议实现
  2. Netty系列(二):Netty拆包/沾包问题的解决方案

新建一个 ChannelMap 类,在客户端第一次连接时保存 channel 连接。后续服务端向客户端发送消息时,先从 Map 中找到对应的客户端消息通道连接,再向通道中写入消息进行发送。


/**
 * @Author 鳄鱼儿
 * @Description 连接通道保存MAP
 * @date 2022/11/27 16:30
 * @Version 1.0
 */

public class ChannelMap {
    /**
     * 存放客户端标识ID(消息ID)与channel的对应关系
     */
    private static volatile ConcurrentHashMap<String, Channel> channelMap = null;

    private ChannelMap() {
    }
    
    public static ConcurrentHashMap<String, Channel> getChannelMap() {
        if (null == channelMap) {
            synchronized (ChannelMap.class) {
                if (null == channelMap) {
                    channelMap = new ConcurrentHashMap<>();
                }
            }
        }
        return channelMap;
    }

    public static Channel getChannel(String id) {
        return getChannelMap().get(id);
    }
}

在客户端建立连接(服务端收到心跳消息)时,将channel加入map中。


public class ServerListenerHandler extends SimpleChannelInboundHandler<Message> {
    private static final Logger log = LoggerFactory.getLogger(ServerListenerHandler.class);

    /**
     * 设备接入连接时处理
     *
     * @param ctx
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        log.info("有新的连接:[{}]", ctx.channel().id().asLongText());
    }

    /**
     * 数据处理
     *
     * @param ctx
     * @param msg
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg) {
        // 获取消息实例中的消息体
        String content = msg.getContent();
        // 对不同消息类型进行处理
        MessageEnum type = MessageEnum.getStructureEnum(msg);
        switch (type) {
            case CONNECT:
                // 将通道加入ChannelMap
                ChannelMap.getChannelMap().put(msg.getId(), ctx.channel());
                
                // 将客户端ID作为自定义属性加入到channel中,方便随时channel中获取用户ID
                AttributeKey<String> key = AttributeKey.valueOf("id");
                ctx.channel().attr(key).setIfAbsent(msg.getId());

                // TODO 心跳消息处理
            case STATE:
                // TODO 设备状态
            default:
                System.out.println(type.content + "消息内容" + content);
        }
    }

    /**
     * 设备下线处理
     *
     * @param ctx
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) {
        log.info("设备下线了:{}", ctx.channel().id().asLongText());
        // map中移除channel
        removeId(ctx);
    }

    /**
     * 设备连接异常处理
     *
     * @param ctx
     * @param cause
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 打印异常
        log.info("异常:{}", cause.getMessage());
        // map中移除channel
        removeId(ctx);
        // 关闭连接
        ctx.close();
    }

    private void removeId(ChannelHandlerContext ctx) {
        AttributeKey<String> key = AttributeKey.valueOf("id");
        // 获取channel中id
        String id = ctx.channel().attr(key).get();
        // map移除channel
        ChannelMap.getChannelMap().remove(id);
    }
}

写一个服务端发送消息的业务层类,并通过客户端id在map中获取到channel通道,将消息转化成json字符串后,通过writeAndFlush发送至客户端。


/**
 * @Author 鳄鱼儿
 * @Description 向客户端发送消息
 * @date 2022/11/27 17:29
 * @Version 1.0
 */

@Service
public class PushMsgServiceImpl implements PushMsgService {

    /**
     * 向一个客户端发送消息
     *
     * @param msg
     */
    @Override
    public void push(Message msg) {
        // 客户端ID
        String id = msg.getId();
        Channel channel = ChannelMap.getChannel(id);
        if (null == channel) {
            throw new RuntimeException("客户端已离线");
        }
        channel.writeAndFlush(msg);
    }
}

注意:writeAndFlush参数是自定义编码的泛型对象实例。如本文自定义的Message消息解析类。

public class MessageEncodeHandler extends MessageToByteEncoder<Message> {

    private static String delimiter;

    public MessageEncodeHandler(String delimiter) {
        this.delimiter = delimiter;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Message message, ByteBuf out) throws Exception {
        out.writeBytes(
                (message.toJsonString() + delimiter)
                        .getBytes(CharsetUtil.UTF_8)
        );
    }
}

之后再编写一个Controller类(这里省略),在Controller类中调用PushMsgService中pushff,就可以完成对客户端的消息发送。

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
以下是一个简单的 Netty 服务发送消息客户的 Java 代码示例: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public class NettyServer { private int port; public NettyServer(int port) { this.port = port; } public void run() throws Exception { NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(new NettyServerHandler()); } }); ChannelFuture future = bootstrap.bind(port).sync(); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { new NettyServer(8080).run(); } private class NettyServerHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 接收到客户消息 System.out.println("接收到客户消息:" + msg); // 向客户发送消息 ctx.writeAndFlush("服务已接收到消息:" + msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } } ``` 在上述代码中,我们创建了一个 Netty 服务,并且在 `NettyServerHandler` 类中实现了处理客户消息的方法 `channelRead0`。在该方法中,我们打印了客户发送消息,并且使用 `ctx.writeAndFlush` 向客户发送了一个回应消息。需要注意的是,在 Netty 中,所有的网络操作都是异步的,因此我们需要使用 `ChannelFuture` 来等待异步操作完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鳄鱼儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值