0

启动 CoAP 服务器后,我需要动态添加新资源。但是我必须停止并再次启动服务器才能访问新资源。我想添加新资源与在已经启动的 HTTP 服务器中添加新的 HTTP servlet 相同。

这里我添加了用于添加动态资源的源代码。如果我在这里遗漏任何东西,请告诉我。

private static CoapServer server;

public CoAPEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
                        Map<String, String> globalProperties) {
    this.eventAdapterConfiguration = eventAdapterConfiguration;
    this.globalProperties = globalProperties;
    if(server == null){
        server = new CoapServer();
        server.start();
    }
}

@Override
public void connect() {
    registerDynamicEndpoint(eventAdapterConfiguration.getName());
    isConnected = true;
} 

private void registerDynamicEndpoint(String adapterName) {
        server.stop();
        server.add(new HelloWorldResource(adapterName));
        server.start();
}


class HelloWorldResource extends CoapResource {

    public HelloWorldResource(String resourceName) {
        // set resource identifier
        super(resourceName);
        // set display name
        getAttributes().setTitle("Hello-World Resource");
    }

    @Override
    public void handleGET(CoapExchange exchange) {

        // respond to the request
        exchange.respond("Hello World!");
    }
}
4

1 回答 1

0

我刚刚尝试动态添加 CoapResource:

@Override
public void handleGET(CoapExchange exchange) {
    server.getRoot().add(new CoapResource("dynstatus") {
        @Override
        public void handleGET(CoapExchange exchange) {
            System.err.println("dynstatus!!!");
        }
    });

并且能够调用它。

于 2017-02-24T14:11:27.343 回答