对于SSDP协议,最简单的Reactor / Netty实现是什么?

时间:2018-12-09 02:23:15

标签: udp spring-webflux project-reactor ssdp reactor-netty

我用UdpClient,UdpServer,Spring Integration Ip模块尝试了很多东西,包装了DatagramSocket接收方法以返回Flux,但是我根本无法从标准SSDP 239.255.255.250:1900接收任何响应。

还向SSDP发送数据包的奖励积分。

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,这是一个片段。我能够通过Flux发射器而不是反应堆网络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<String> 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()))