This library is a transport agnostic implementation of the Constraint Application Protocol (CoAP - RFC 7252) for .Net Standard.
Since CoAP is designed for unreliable transport layers. (6LoWPAN, UDP, etc...) it made sense to not worry about the transport implementaions and allow the applicatrion to provide their own.
If you're after a UDP transport example, see CoAPNet.Udp
All relevant changes are logged in Changelog.md
-
Full support for RFC 7959 - Block-Wise Transfers in the Constrained Application Protocol (CoAP)
-
Support for Sending and Receiving Confirmable (CON) and Non-Confirmable (NON) messagees.
- Retransmit confirmable messages, throwing an exception on failure
- Rejects malform messages with appropiate error code.
- Ignores repeated messages within a set
TimeSpan
-
Support for Sending and receiving multicast messages.
-
CoapServer
- Simple server for binding to local transports and processing requestsCoapHandler
- Template request handler to be used by CoapServerCoapResourceHandler
- Example handler that specificaly servesCoapResource
s
- Create unit tests to cover as much of RFC7252 as possible.
- Create more examples
- Support DTLS over UDP
- Resolve host names using mDNS or DNS
- Await message response for non-confirmable (NON) messages
- Support for RFC 7641 - Observing Resources in the Constrained Application Protocol (CoAP)
- Support for RFC 7390 - Group Communication for the Constrained Application Protocol (CoAP)
using System;
using System.Text;
using System.Threading.Tasks;
using CoAPNet;
using CoAPNet.Udp;
namespace CoAPDevices
{
class Program
{
static void Main(string[] args)
{
Task.Run(async () =>
{
// Create a new client using a UDP endpoint (defaults to 0.0.0.0 with any available port number)
var client = new CoapClient(new CoapUdpEndPoint());
var messageId = await client.GetAsync("coap://127.0.0.1/hello");
var response = await client.GetResponseAsync(messageId);
Console.WriteLine("got a response");
Console.WriteLine(Encoding.UTF8.GetString(response.Payload));
}).GetAwaiter().GetResult();
Console.WriteLine("Press <Enter> to exit");
Console.ReadLine();
}
}
}
using System;
using System.Net;
using System.Text;
using System.Threading;
using CoAPNet;
using CoAPNet.Options;
using CoAPNet.Udp;
namespace CoAPDevices
{
class Program
{
static void Main(string[] args)
{
var failed = false;
var myHandler = new CoapResourceHandler();
myHandler.Resources.Add(new HelloResource("/hello"));
var myServer = new CoapServer(new CoapUdpTransportFactory());
try
{
myServer.BindTo(new CoapUdpEndPoint(IPAddress.Loopback, Coap.Port));
myServer.BindTo(new CoapUdpEndPoint(IPAddress.IPv6Loopback, Coap.Port));
myServer.StartAsync(myHandler, CancellationToken.None).GetAwaiter().GetResult();
Console.WriteLine("Server Started!");
Console.WriteLine("Press <Enter> to exit");
Console.ReadLine();
}
catch (Exception ex)
{
failed = true;
Console.WriteLine($"{ex.GetType().Name} occured: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
finally
{
Console.WriteLine("Shutting Down Server");
myServer.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
}
if (failed)
Console.ReadLine();
}
}
public class HelloResource : CoapResource
{
public HelloResource(string uri) : base(uri)
{
Metadata.InterfaceDescription.Add("read");
Metadata.ResourceTypes.Add("message");
Metadata.Title = "Hello World";
}
public override CoapMessage Get(CoapMessage request)
{
return new CoapMessage
{
Code = CoapMessageCode.Content,
Options = {new ContentFormat(ContentFormatType.TextPlain)},
Payload = Encoding.UTF8.GetBytes("Hello World!")
};
}
}
}