1 Star 1 Fork 1

IoTSharp/IoTSharp.CoAP.NET

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

CoAP.NET - A CoAP framework in C#

Build status

' Install-Package IoTSharp.CoAP.NET -Version 2.0.8

The Constrained Application Protocol (CoAP) (https://datatracker.ietf.org/doc/draft-ietf-core-coap/) is a RESTful web transfer protocol for resource-constrained networks and nodes. CoAP.NET is an implementation in C# providing CoAP-based services to .NET applications. Reviews and suggestions would be appreciated.

Content

  • [Quick Start] (#quick-start)
  • [Build] (#build)
  • [License] (#license)
  • [Acknowledgements] (#acknowledgements)

Quick Start

CoAP sessions are considered as request-response pair.

CoAP Client

Access remote CoAP resources by issuing a [Request] (CoAP.NET/Request.cs) and receive its [Response] (CoAP.NET/Request.cs)(s).

  // new a GET request
  Request request = new Request(Method.GET);
  request.URI = new Uri("coap://[::1]/hello-world");
  request.Send();
  
  // wait for one response
  Response response = request.WaitForResponse();

There are 4 types of request: GET, POST, PUT, DELETE, defined as Method.GET, Method.POST, Method.PUT, Method.DELETE.

Responses can be received in two ways. By calling request.WaitForResponse() a response will be received synchronously, which means it will block until timeout or a response is arrived. If more responses are expected, call WaitForResponse() again.

To receive responses asynchronously, register a event handler to the event request.Respond before executing.

Parsing Link Format

Use LinkFormat.Parse(String) to parse a link-format response. The returned enumeration of WebLink contains all resources stated in the given link-format string.

Request request = new Request(Method.GET); request.URI = new Uri("coap://[::1]/.well-known/core"); request.Send(); Response response = request.WaitForResponse(); IEnumerable links = LinkFormat.Parse(response.PayloadString);


See [CoAP Example Client] (CoAP.Example/CoAP.Client) for more.

### CoAP Server

A new CoAP server can be easily built with help of the class
[**CoapServer**] (CoAP.NET/Server/CoapServer.cs)

```csharp
static void Main(String[] args)
{
  CoapServer server = new CoapServer();
  
  server.Add(new HelloWorldResource("hello"));
  
  server.Start();
  
  Console.ReadKey();
}

See [CoAP Example Server] (CoAP.Example/CoAP.Server) for more.

CoAP Resource

CoAP resources are classes that can be accessed by a URI via CoAP. In CoAP.NET, a resource is defined as a subclass of [Resource] (CoAP.NET/Server/Resources/Resource.cs). By overriding methods DoGet, DoPost, DoPut or DoDelete, one resource accepts GET, POST, PUT or DELETE requests.

The following code gives an example of HelloWorldResource, which can be visited by sending a GET request to "/hello-world", and respones a plain string in code "2.05 Content".

  class HelloWorldResource : Resource
  {
      public HelloWorldResource()
          : base("hello-world")
      {
          Attributes.Title = "GET a friendly greeting!";
      }

      protected override void DoGet(CoapExchange exchange)
      {
          exchange.Respond("Hello World from CoAP.NET!");
      }
  }
  
  class Server
  {
      static void Main(String[] args)
      {
          CoapServer server = new CoapServer();
          server.Add(new HelloWorldResource());
          server.Start();
      }
  }

See [CoAP Example Server] (CoAP.Server) for more.

Build

A few compile symbols are introduced to build for different drafts of CoAP:

By default (with no symbol defined), CoAP.NET will be compiled with the latest version of CoAP protocol. To enable drafts, define one or more of those compile symbols.

With drafts enabled, an interface ISpec will be introduced, representing draft specification. Define COAPXX to enable draft XX, or COAPALL to enable all supported drafts. All enabled drafts will be available in class [Spec] (CoAP.NET/Spec.cs):

  public static class Spec
  {
    public static readonly ISpec Draft03;
    public static readonly ISpec Draft08;
    public static readonly ISpec Draft12;
    public static readonly ISpec Draft13;
    public static readonly ISpec Draft18;
  }

With none of the symbols defined, only the latest version of draft will be compiled as the class [Spec] (CoAP.NET/Spec.cs), with static members instead of various drafts:

  public static class Spec
  {
    public static readonly String Name = "draft-ietf-core-coap-18";
    public static readonly Int32 DefaultPort = 5683;
    public static IMessageEncoder NewMessageEncoder();
    public static IMessageDecoder NewMessageDecoder(Byte[] data);
    public static Byte[] Encode(Message msg);
    public static Message Decode(Byte[] bytes);
  }

License

See [LICENSE] (LICENSE) for more info.

Acknowledgements

CoAP.NET is based on [Californium] (https://github.com/mkovatsc/Californium), a CoAP framework in Java by Matthias Kovatsch, Dominique Im Obersteg, and Daniel Pauli, ETH Zurich. See http://people.inf.ethz.ch/mkovatsc/californium.php. Thanks to the authors and their great job.

Copyright (c) 2011-2013, Longxiang He <longxianghe@gmail.com>, SmeshLink Technology Co. Copyright (c) 2019-2020, maikebing <mysticboy@live.com> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

A C# implementation of the CoAP protocol 展开 收起
README
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/IoTSharp/IoTSharp.CoAP.NET.git
git@gitee.com:IoTSharp/IoTSharp.CoAP.NET.git
IoTSharp
IoTSharp.CoAP.NET
IoTSharp.CoAP.NET
master

搜索帮助