Giter Club home page Giter Club logo

coap-csharp's Introduction

CoAP.NET - A CoAP framework in C#

NuGet Status Build Status

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.

Copyright

Copyright (c) 2011-2015, Longxiang He [email protected], SmeshLink Technology Co.

Content

How to Install

The C# implementation is available in the NuGet Package Gallery under the name Com.AugustCellars.CoAP. To install this library as a NuGet package, enter 'Install-Package Com.AugustCellars.CoAP' in the NuGet Package Manager Console.

Documentation

Documentation can be found in two places. First an XML file is installed as part of the package for inline documentation. Additionally, I have started working on the Wiki associated with this project.

Quick Start

CoAP sessions are considered as request-response pair.

CoAP Client

Access remote CoAP resources by issuing a Request and receive its Response(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<WebLink> links = LinkFormat.Parse(response.PayloadString);

See CoAP Example Client for more.

CoAP Server

A new CoAP server can be easily built with help of the class CoapServer

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

See CoAP Example 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. 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 for more.

Building the sources

I am currently sync-ed up to Visual Studio 2017 and have started using language features of C# v7.0 that are supported both in Visual Studio and in the latest version of mono.

License

See LICENSE for more info.

Acknowledgements

This is a copy of the CoAP.NET project hosted at (https://http://coap.codeplex.com/). As this project does not seem to be maintained anymore, and I am doing active updates to it, I have made a local copy that things are going to move forward on.

Current projects are:

CoAP.NET is based on 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.

coap-csharp's People

Contributors

jimsch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.