繁体   English   中英

无法接收来自设备的 UDP 响应

[英]Unable to receive the UDP response from device

我正在尝试发送 UDP 请求并接收响应。 Spring 集成具有此类任务的适当工具: UnicastSendingMessageHandlerUnicastReceivingChannelAdapter 我通过以下方式配置它

@Bean
public MessageChannel requestChannel() {
    return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel = "requestChannel")
public UnicastSendingMessageHandler unicastSendingMessageHandler() {
    UnicastSendingMessageHandler unicastSendingMessageHandler = new UnicastSendingMessageHandler("239.255.255.250", 1982);
    return unicastSendingMessageHandler;
}

@Bean
public UnicastReceivingChannelAdapter unicastReceivingChannelAdapter() {
    UnicastReceivingChannelAdapter unicastReceivingChannelAdapter = new UnicastReceivingChannelAdapter(8080);
    unicastReceivingChannelAdapter.setOutputChannelName("nullChannel");
    return unicastReceivingChannelAdapter;
}

我如何发送消息(我在任何我想要的地方使用sendDiscoveryMessage() ):

@Service
public class DiscoveryService {

    private static final String DISCOVERY_MESSAGE = "M-SEARCH * HTTP/1.1\r\n"
            + "HOST: 239.255.255.250:1982\r\n"
            + "MAN: \"ssdp:discover\"\r\n"
            + "ST: wifi_bulb";

    private final MessageChannel requestChannel;

    public DiscoveryService(final MessageChannel requestChannel) {
        this.requestChannel = requestChannel;
    }

    public void sendDiscoveryMessage() {
        requestChannel.send(new GenericMessage<>(DISCOVERY_MESSAGE));
    }
}

此时,我可以通过 WireShark 检查数据包,并确保已发送数据报并发送适当的响应。 唯一的问题是如何接收此响应。 据我了解阅读文档,我需要使用@ServiceActivator注释的方法。 但我不明白我应该在哪里(哪个频道)收到响应(为了正确指定@ServiceActivator(inputChannel="") )。 另外,我不确定我为UnicastSendingMessageHandler bean 放置的@ServiceActivator(inputChannel = "requestChannel") 我尝试创建以下方法(假设响应将来自同一个频道):

@ServiceActivator(inputChannel = "requestChannel")
public void receiveResponse(Message<String> response) {
    System.out.println(response);
}

但它实际上拦截了我自己的请求消息(对我来说似乎合乎逻辑,因为我将请求发送到requestChannel )。 所以我不明白我需要多少个通道(也许我需要 1 个请求和 1 个响应)以及如何创建@ServiceActivator来捕获响应。

unicastReceivingChannelAdapter.setOutputChannelName("nullChannel");

您将结果发送到nullChannel ,就像 Unix 上的/dev/null一样; 你正在丢弃它。

使用@ServiceActivator(inputChannel = "replyChannel")

unicastReceivingChannelAdapter.setOutputChannelName("replyChannel");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM