ZIP《整合 Spring Boot 和 Netty 实现 WebSocket》 代码写的太完美了!存一个! 6.55KB

liuruiaaa

资源文件列表:

nettyweb.zip 大约有7个文件
  1. NettyServer.java 2.91KB
  2. ProjectInitializer.java 2.01KB
  3. PushMsgService.java 453B
  4. PushMsgServiceImpl.java 957B
  5. TestController.java 2.25KB
  6. WebSocketHandler.java 3.21KB
  7. NettyConfig.java 1.81KB

资源介绍:

《整合 Spring Boot 和 Netty 实现 WebSocket》 代码写的太完美了!存一个! 资源来自:https://blog.csdn.net/dot_life/article/details/136575529
package io.renren.modules.test.nettyweb; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.util.AttributeKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; /** * @author dongliang7 * @projectName websocket-parent * @ClassName WebSocketHandler.java * @description: TODO * @createTime 2023年02月06日 16:44:00 */ @Component @ChannelHandler.Sharable public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { private static final Logger log = LoggerFactory.getLogger(NettyServer.class); /** * 一旦连接,第一个被执行 */ @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { log.info("有新的客户端链接:[{}]", ctx.channel().id().asLongText()); // 添加到channelGroup 通道组 NettyConfig.getChannelGroup().add(ctx.channel()); } /** * 读取数据 */ @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { log.info("服务器收到消息:{}", msg.text()); // 获取用户ID,关联channel JSONObject jsonObject = JSONUtil.parseObj(msg.text()); String uid = jsonObject.getStr("uid"); String touid = jsonObject.getStr("touid"); NettyConfig.getChannelMap().put(uid, ctx.channel()); // 将用户ID作为自定义属性加入到channel中,方便随时channel中获取用户ID AttributeKey<String> key = AttributeKey.valueOf("userId"); ctx.channel().attr(key).setIfAbsent(uid); // 回复消息 ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器收到消息啦")); //然后找找要发给谁 Channel channelToUid = NettyConfig.getChannelMap().get(touid); if(!ObjectUtils.isEmpty(channelToUid)){ channelToUid.writeAndFlush(new TextWebSocketFrame(jsonObject.getStr("message")) ); } } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { log.info("用户下线了:{}", ctx.channel().id().asLongText()); // 删除通道 NettyConfig.getChannelGroup().remove(ctx.channel()); removeUserId(ctx); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { log.info("异常:{}", cause.getMessage()); // 删除通道 NettyConfig.getChannelGroup().remove(ctx.channel()); removeUserId(ctx); ctx.close(); } /** * 删除用户与channel的对应关系 */ private void removeUserId(ChannelHandlerContext ctx) { AttributeKey<String> key = AttributeKey.valueOf("userId"); String userId = ctx.channel().attr(key).get(); NettyConfig.getChannelMap().remove(userId); } }
100+评论
captcha
    类型标题大小时间
    ZIPAndroidStudio期末设计天气预报APP源代码+实验报告25.71MB9月前
    ZIP纯netty,没有websocket5.9KB9月前
    ZIPQt学习记录(十)项目:数字炸弹/猜数字游戏12.93KB9月前
    ZIP计算机组成原理实验8位可控加减法电路设计,原码一位乘法器设计,MIPS运算器设计,汉字字库存储芯片扩展实验,寄存器文件设1.55MB9月前
    ZIPQT利用QMovie实现动态加载、等待提示框7.75KB9月前
    ZIPC# 程序集的加载和卸载101.83KB9月前
    ZIPnumpy for android编译配置补丁925B9月前
    ZIPIxChariot测速工具35.07MB9月前