0

我想通过 udp 客户端一次发送大量 udp 消息,但演示只发送一条消息。我该如何实现呢?

使用演示代码,我只能发送有限数量的消息。我想使用一段时间(真)来发送消息,我该如何实现呢?

public static void main(String[] args) { Connection connection = UdpClient.create() .host("localhost") .port(8080) .handle((udpInbound, udpOutbound) -> { return udpOutbound.sendString(Mono.just ("end")).sendString(Mono.just("end1")).sendString(Mono.just("end2")); }) .connectNow(Duration.ofSeconds(30)); 连接.onDispose() .block(); }

4

1 回答 1

1

You can use Flux instead of Mono when you want to send more than one message. One sendString(Flux)invocation is better in comparison with the approach with many sendString(Mono) invocations. The example below uses Flux.interval so that you have infinite stream that emits messages every 100ms. Also when you have an infinite stream you have to switch to flush on each strategy

Connection connection =
        UdpClient.create()
                 .host("localhost")
                 .port(8080)
                 .handle((udpInbound, udpOutbound) ->
                         udpOutbound.options(NettyPipeline.SendOptions::flushOnEach)
                                    .sendString(Flux.interval(Duration.ofMillis(100))
                                                    .map(l -> l + "")))
                 .connectNow(Duration.ofSeconds(30));
于 2019-04-08T10:35:29.100 回答