Getting Started

An overview of CoAP.NET, how to install and use.


Installing CoAP.NET

CoAP.NET is available on NuGet.

PM> Install-Package CoAP

Or download pre-compiled binaries from releases page.

Currently v1.1.0.

Using CoAP Client

// create a new client
var client = new CoapClient();

// set the Uri to visit
client.Uri = new Uri("coap://SERVER_ADDRESS/helloworld");

// now send a GET request to say hello~
var response = client.Get();

Console.WriteLine(response.PayloadString);  // Hello World!

view example

Using CoAP Server

First, creates a new resource of your own:

class HelloWorldResource : Resource
{
	// use "helloworld" as the path of this resource
	public HelloWorldResource() : base("helloworld")
	{
		// set a friendly title
		Attributes.Title = "GET a friendly greeting!";
	}
	
	// override this method to handle GET requests
	protected override void DoGet(CoapExchange exchange)
	{
		// now we get a request, respond it
		exchange.Respond("Hello World!");
	}
}

Then, starts a new CoAP server and add the new resource:

// create a new server
var server = new CoapServer();

// add the resource to share
server.Add(new HelloWorldResource());

// let the server fly
server.Start();

Now the resource is gettable on coap://SERVER_ADDRESS/helloworld.

view example

Building from Source

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

By default (without any 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.


Next: Documentation