Commit da54c2c1 authored by 赵啸非's avatar 赵啸非

修改消息组件

parent c3b17e1e
......@@ -29,7 +29,7 @@ public class NettyStartedService implements IApplicationStartedService {
public void start() {
logger.info("开始netty服务....");
thread = new Thread(() -> {
controlServer.run(7688);
controlServer.run(7788);
});
thread.setDaemon(true);
thread.start();
......
......@@ -5,6 +5,7 @@ import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.device.service.DeviceService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
......@@ -34,8 +35,13 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
System.out.println("111111111");
log.info("receive->UDP:{}",msg.content().toString(CharsetUtil.UTF_8));
DatagramPacket packet = (DatagramPacket) msg;
ByteBuf byteBuf = packet.copy().content();
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
String content = new String(bytes);
log.info("receive->UDP:{}", packet.sender().toString() + "," + content);
// TODO 设备返回服务端配置
String URL = GlobalSysInfo.getParamValue(PARAM_SERVER_HTTP_URL, "http://192.168.0.98:11021");
......@@ -44,7 +50,7 @@ public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramP
resp.setCode(YesNoEnum.YES.getValue());
resp.setMsg("获取服务端地址成功!");
resp.setData(URL);
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(JSON.toJSONString(resp), CharsetUtil.UTF_8),new InetSocketAddress(7689), msg.sender()));
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(JSON.toJSONString(resp), CharsetUtil.UTF_8), msg.sender()));
}
......
......@@ -25,7 +25,7 @@ public class UDPClientApp {
public void init() throws IOException {
try (DatagramSocket sock = new DatagramSocket()) {
outpack = new DatagramPacket(new byte[0], 0,new InetSocketAddress(7788));
outpack = new DatagramPacket(new byte[0], 0,new InetSocketAddress(InetAddress.getByName("192.168.0.1"),7688));
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()) { // 键盘输入
byte[] buf = scan.nextLine().getBytes(); // 捕获发送
......@@ -40,21 +40,19 @@ public class UDPClientApp {
public static void main(String[] args) throws IOException {
//new UDPClientApp().init();
// new UDPClientApp().init();
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup workGroup = new NioEventLoopGroup();
bootstrap.group(workGroup).channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new UDPClientHandler());
}
});
try {
Channel channel = bootstrap.bind(0).sync().channel();
Channel channel = bootstrap.bind(7789).sync().channel();
channel.closeFuture().sync().await();
} catch (Exception e) {
e.printStackTrace();
......
......@@ -2,6 +2,7 @@ package com.mortals.httpclient;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
......@@ -26,8 +27,34 @@ public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("hello,server111", Charset.forName("UTF-8")),
new InetSocketAddress(7688)));
log.info("remoteAddress:{}", ctx.channel().remoteAddress());
ctx.executor().parent().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("我在广播" + i, Charset.forName("utf-8")),
new InetSocketAddress("255.255.255.255", 7788)));
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
/* ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("hello,server", Charset.forName("UTF-8")),
new InetSocketAddress(54321)));*/
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("channelInactive remoteAddress:{}", ctx.channel().remoteAddress());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment