无法通过 jupnp 在 Java 中发送 UPnP 搜索消息

问题描述 投票:0回答:1

我正在尝试使用 jupnp 在 Java 中发送 UPnP 搜索消息。但是,文档似乎已过时,并且尝试发送搜索消息会导致 ControlPoint 为空,因此我无法发送消息。这是我到目前为止的代码:

package xyz.necrozma.upnpplugin;

import org.bukkit.plugin.java.JavaPlugin;

import org.jupnp.DefaultUpnpServiceConfiguration;
import org.jupnp.controlpoint.ControlPoint;
import org.jupnp.model.message.header.STAllHeader;
import org.jupnp.UpnpService;
import org.jupnp.UpnpServiceImpl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class main extends JavaPlugin {

    private final Logger logger = LoggerFactory.getLogger(main.class);
    private UpnpService upnpService;

    private int port = 25565;

    @Override
    public void onEnable() {

        port = getServer().getPort();
        logger.info("Trying to open port " + port + " for UPNP");


        upnpService = new UpnpServiceImpl(new DefaultUpnpServiceConfiguration());

        logger.info("UPNP service created, searching for devices, please wait...");

        ControlPoint controlPoint = upnpService.getControlPoint();

        if (controlPoint != null) {
            controlPoint.search(new STAllHeader());

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            logger.info("Search complete, found " + upnpService.getRegistry().getRemoteDevices().size() + " devices.");

        } else {
            logger.error("ControlPoint is null, unable to perform search.");
        }

        upnpService.getRegistry().getRemoteDevices().forEach((remoteDevice) -> {
            logger.info("Found device: " + remoteDevice.getDisplayString());
        });

        }

    @Override
    public void onDisable() {
        logger.info("UPNP service stopping...");
        if (upnpService != null) {
            upnpService.shutdown(); // Shut down the UPnP service
        }
    }
}

我希望有人对如何使用这个库有一些经验或知识,因为我希望得到一些指导。

java upnp spigot
1个回答
0
投票

阅读其他示例后,我通过在创建服务后添加

upnpService.startup();
修复了 null ControlPoint 错误。

© www.soinside.com 2019 - 2024. All rights reserved.