0
votes

I want to send numerous udp messages by udp client at once ,but the demo only send one message.how could i achieve it?

With the demo code,I can only send a finite number of messages.I want to use a while(true) to send message ,how could i achieve it?

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)); connection.onDispose() .block(); }

1

1 Answers

1
votes

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));