简体   繁体   English

如何为CoAP请求添加其他选项?

[英]How to add extra options to CoAP request?

I know that CoAP defines some options which can be included in the sending request and each option has their own number. 我知道CoAP定义了一些可以包含在发送请求中的选项,每个选项都有自己的编号。 The structure of the CoAP uri request looks like: CoAP uri请求的结构如下:

coap-URI = "coap:" "//" host [ ":" port ] path-abempty [ "?" coap-URI =“ coap:”“ //”主机[“:”端口]路径豁免[“?” query ] 查询

where inside they include some options like: Uri-Host, Uri-Port, Uri-Path, and Uri-Query, and each of them has their own number, ex: 3 for Uri-Host, 11 for Uri Path.... . 在其中包含一些选项,例如:Uri-Host,Uri-Port,Uri-Path和Uri-Query,它们每个都有自己的编号,例如:Uri-Host为3,Uri Path为11。 。 And I would like to add some more extra options to this CoAP request, for example some options number 256, 257...How can I do that? 我想为此CoAP请求添加更多其他选项,例如一些选项编号256、257 ...我该怎么做?

Thank you in advanced 谢谢高级

Son 儿子

I've managed to pass the Option Number 256. 我设法通过了选件编号256。

CoapClient client = new CoapClient(...);
Request request = new Request(CoAP.Code.GET, CoAP.Type.NON);
OptionSet optionSet = new OptionSet();
optionSet.addOption(new Option(256, "admin:admin"));
request.setOptions(optionSet);
client.advanced(request); // or async version
client.shutdown();

At resource: 在资源上:

@Override
public void handleGET(CoapExchange exchange) {
    OptionSet optionSet = exchange.advanced().getRequest().getOptions();
    List<Option> options = optionSet.asSortedList();
    options.stream()
            .filter(o -> o.getNumber() == 256)
            .findFirst()
            .ifPresent(o -> System.err.println(o.getNumber() + " " + o.getStringValue()));
}

Output: 输出:

256 admin:admin

However, Option Number 256 may not be a proper choice in general: 但是,一般而言,选项号256可能不是一个合适的选择:

RFC 7252 The Constrained Application Protocol (CoAP). RFC 7252约束应用协议(CoAP)。 12.2. 12.2。 CoAP Option Numbers Registry CoAP选件编号注册表

The IANA policy for future additions to this sub-registry is split into three tiers as follows. IANA未来对该子域添加内容的政策分为以下三层。 The range of 0..255 is reserved for options defined by the IETF (IETF Review or IESG Approval). 0..255的范围保留给IETF(IETF审查或IESG批准)定义的选项。 The range of 256..2047 is reserved for commonly used options with public specifications (Specification Required). 256..2047的范围保留用于具有公共规范(需要规范)的常用选项。 The range of 2048..64999 is for all other options including private or vendor-specific ones, which undergo a Designated Expert review to help ensure that the option semantics are defined correctly. 2048..64999的范围适用于所有其他选项,包括专用选项或特定于供应商的选项,这些选项均经过指定专家审核,以帮助确保正确定义选项语义。 The option numbers between 65000 and 65535 inclusive are reserved for experiments. 保留65000到65535之间的选件号供实验使用。 They are not meant for vendor-specific use of any kind and MUST NOT be used in operational deployments. 它们不适合用于任何特定于供应商的用途,并且绝不能在运营部署中使用。

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

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