对于那些感兴趣的人,这是一个片段 . 我能够通过Flux Launcher 而不是reactor-netty UDP类来实现 .
private static final String SSDP_IP = “239.255.255.250”;
private static final int SSDP_PORT = 1900;
private static final int TIMEOUT = 5000;
String request = new String(“example”);
byte[] receiveData = new byte[1024];
DatagramPacket sendPacket = new DatagramPacket(request.getBytes(),
request.getBytes().length, InetAddress.getByName(SSDP_IP), SSDP_PORT);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length,
InetAddress.getLocalHost(), SSDP_PORT);
Flux response = Flux.create(emitter -> {
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT);
socket.send(sendPacket);
while (true) {
socket.receive(receivePacket);
emitter.next(receivePacket.getData());
}
} catch (SocketTimeoutException e) {
emitter.complete();
} catch (IOException e) {
emitter.error(e);
}
})
.map(mapper -> new String(receivePacket.getData(), 0, receivePacket.getLength()))
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183164.html原文链接:https://javaforall.cn