[英]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.