使用 californium java 库的 CoAP 协议

物联网 联网
2021-05-29 14:17:12

我正在使用 java 使用 CoAP californium 库,我的问题是我想在服务器的主要功能中从客户端获取请求的资源。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    // binds on UDP port 5683
    CoapServer server=new CoapServer();
    // "hello"
    server.add(new HelloResource());
    // "subpath/Another"
    CoapResource path=new CoapResource("subpath");
    path.add(new AnotherResource());
    server.add(path);
    server.add(new RemovableResource(),new TimeResource(),new WritableResource());
    System.out.println("Resources added");
    server.start();
}

之后我应该写什么server.start() ..我知道服务器使用函数deliverRequest(final Exchange exchange)函数并findResource(final List<String> list)在资源树中搜索请求的资源..但我想获取这个值并将其存储在服务器的主函数中的变量中

1个回答

Californium 旨在同时处理多个请求。因此它使用回调(CoapResource)。可能是我误解了你这个问题的意图。对我来说,“all in main”听起来像是一种“阻塞函数等待传入请求并返回它们”的方法。这不是与 californium 一起使用的。