繁体   English   中英

Coap 服务器资源发现

[英]Coap server resources discovery

我正在使用 californium 库进行 coap 通信,它正在部署在 Android 平台上。 我已经在一台设备上启动了 coap 服务器,而客户端在另一台设备上,两者都在同一个网络中。

服务器代码:使用以下资源创建服务器

class HelloWorldResource extends CoapResource {

    public HelloWorldResource() {

        // set resource identifier
        super("hello");

        // set display name
        getAttributes().setTitle("Hello-World Resource");
    }

    @Override
    public void handleGET(CoapExchange exchange) {

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

客户端代码:

    CoapClient coapClient = new CoapClient("coap://localhost/.well-known/core");

    try {
        Set<WebLink> webLinks = coapClient.discover();
        System.out.println(webLinks.size());
    } catch (ConnectorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

上述代码中没有 output。 不知道IP地址,想和服务器通信。 这是正确的方法还是我错过了什么?

“发现”是一个特殊的 coap 请求,用于发现已知主机的资源。

要发现主机,可以使用多播请求,但必须得到服务器的支持。

“localhost”是设备本身的地址。 这样,设备将向自身发送发现请求。

不知道IP地址,想和服务器通信。

如果您不知道地址,您可以先使用“多播”请求(还需要在服务器端进行准备,请参阅MulticastServer 示例。该示例比您需要的更复杂。但也演示了原理) . 或者您需要查找该地址(android:设置-> 连接-> WLAN-> 设置-图标:在那里您可以看到您的 IP 地址。

coapClient.discover()

发现“已知”服务器,而不是本地网络。 将 localhost 替换为“californium.eclipseprojects.io”,您将获得:

</.well-known/core>
</large> Large resource
    rt: [block]
    sz: [1280]
</large-create> Large resource that can be created using POST method
    rt: [block]
</large-post> Handle POST with two-way blockwise transfer
    rt: [block]
</large-separate> Large resource
    rt: [block]
    sz: [1280]
</large-update> Large resource that can be updated using PUT method
    rt: [block]
    sz: [1280]
</link1> Link test resource
    rt: [Type1, Type2]
    if: [If1]
</link2> Link test resource
    rt: [Type2, Type3]
    if: [If2]
</link3> Link test resource
    rt: [Type1, Type3]
    if: [foo]
</location-query> Perform POST transaction with responses containing several Location-Query options (CON mode)
</multi-format> Resource that exists in different content formats (text/plain utf8 and application/xml)
    ct: [0, 41, 50, 60]
</obs> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-large> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-non> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-pumping> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-pumping-non> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-reset>
</path> Hierarchical link description entry
    ct: [40]
</path/sub1> Hierarchical link description sub-resource
</path/sub2> Hierarchical link description sub-resource
</path/sub3> Hierarchical link description sub-resource
</query> Resource accepting query parameters
</seg1> Long path resource
</seg1/seg2> Long path resource
</seg1/seg2/seg3> Long path resource
</separate> Resource which cannot be served immediately and which cannot be acknowledged in a piggy-backed way
</shutdown>
</test> Default test resource
</validate> Resource which varies
    ct: [0]
    sz: [20]

所有收到的链接都与您将发现发送到的服务器相关。

暂无
暂无

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

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