SocketHandler.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.zd.netty.handler;
  2. import com.zd.common.core.utils.SpringUtils;
  3. import com.zd.common.core.utils.StringUtils;
  4. import com.zd.common.redis.service.RedisService;
  5. import io.netty.channel.Channel;
  6. import io.netty.channel.ChannelHandlerContext;
  7. import io.netty.channel.ChannelInboundHandlerAdapter;
  8. import io.netty.channel.group.ChannelGroup;
  9. import io.netty.channel.group.DefaultChannelGroup;
  10. import io.netty.util.concurrent.GlobalEventExecutor;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import java.util.concurrent.ConcurrentHashMap;
  14. import java.util.concurrent.TimeUnit;
  15. /**
  16. * Socket拦截器,用于处理客户端的行为
  17. *
  18. * @author dgs
  19. **/
  20. public class SocketHandler extends ChannelInboundHandlerAdapter {
  21. protected static final ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
  22. private static final Logger log = LoggerFactory.getLogger(SocketHandler.class);
  23. private static ConcurrentHashMap<String, ChannelHandlerContext> map = new ConcurrentHashMap<>();
  24. private static final String RFID_CODE="RFID:";
  25. /**
  26. * 读取到客户端发来的消息
  27. *
  28. * @param ctx ChannelHandlerContext
  29. * @param msg msg
  30. */
  31. @Override
  32. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  33. byte[] data = (byte[]) msg;
  34. String str=byte2Hex(data);
  35. if(StringUtils.isEmpty(str)){
  36. return;
  37. }
  38. String upperCase=str.toUpperCase();
  39. log.info("转换后消息:{}",upperCase);
  40. upperCase=upperCase.substring(8,32);
  41. log.info("截取后:{}",upperCase);
  42. RedisService redisService = SpringUtils.getBean(RedisService.class);
  43. String t= redisService.getCacheObject(RFID_CODE+upperCase);
  44. if(StringUtils.isNotNull(t)){
  45. return;
  46. }
  47. redisService.setCacheObject(RFID_CODE+upperCase,upperCase,5L, TimeUnit.MINUTES);
  48. log.info("需要存入数据");
  49. synchronized (this){
  50. // 根据实际业务应用实现下方接口,进行业务操作,可根据type多方实现,多方发送接收到的编码
  51. // TODO document why this method is empty
  52. // ISendService sendService = SpringUtils.getBean(ISendService.class);
  53. // sendService.send(upperCase,0);
  54. }
  55. }
  56. /**
  57.    * 将byte转为16进制
  58.    * @param bytes
  59.    * @return
  60.    */
  61. public static String byte2Hex(byte[] bytes){
  62. StringBuilder builder = new StringBuilder();
  63. String temp;
  64. for (byte aByte : bytes) {
  65. temp = Integer.toHexString(aByte & 0xFF);
  66. if (temp.length() == 1) {
  67. //1得到一位的进行补0操作
  68. builder.append("0");
  69. }
  70. builder.append(temp);
  71. }
  72. return builder.toString();
  73. }
  74. @Override
  75. public void handlerAdded(ChannelHandlerContext ctx) {
  76. Channel channel = ctx.channel();
  77. String s = channel.id().asShortText();
  78. log.info("新的客户端链接:{}" , s);
  79. clients.add(channel);
  80. String uuid = ctx.channel().id().asLongText();
  81. map.put(uuid, ctx);
  82. }
  83. @Override
  84. public void handlerRemoved(ChannelHandlerContext ctx) {
  85. clients.remove(ctx.channel());
  86. String uuid = ctx.channel().id().asLongText();
  87. map.remove(uuid);
  88. }
  89. @Override
  90. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
  91. cause.printStackTrace();
  92. ctx.channel().close();
  93. clients.remove(ctx.channel());
  94. }
  95. }