下载资源后端资源详情
nettyweb.zip
大小:6.55KB
价格:45积分
下载量:0
评分:
5.0
上传者:liuruiaaa
更新日期:2024-07-15

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

资源文件列表(大概)

文件名
大小
NettyServer.java
2.91KB
ProjectInitializer.java
2.01KB
PushMsgService.java
453B
PushMsgServiceImpl.java
957B
TestController.java
2.25KB
WebSocketHandler.java
3.21KB
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.Sharablepublic 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); }}

用户评论 (0)

发表评论

captcha