package com.ard.utils.udp;
|
|
import com.ard.alarm.tube.service.TubeAlarmService;
|
import com.ard.utils.other.SpringTool;
|
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.Unpooled;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.socket.DatagramPacket;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.nio.charset.Charset;
|
|
@Slf4j(topic = "tube")
|
public class NettyUdpHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
|
@Override
|
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) {
|
try {
|
ByteBuf byteBuf = packet.content();
|
String str = byteBuf.toString(Charset.forName("GBK"));
|
log.info("udp收到数据: " + str);
|
TubeAlarmService tubeAlarmService = (TubeAlarmService) SpringTool.getApplicationContext().getBean("tubeAlarmService");
|
tubeAlarmService.alarmHandler(str);
|
String resStr = "ok";
|
byte[] resBytes = resStr.getBytes(Charset.forName("GBK"));
|
DatagramPacket resData = new DatagramPacket(Unpooled.copiedBuffer(resBytes), packet.sender());
|
ctx.writeAndFlush(resData);
|
} catch (Exception e) {
|
log.error("udp接收数据异常:" + e.getMessage());
|
}
|
}
|
}
|