CoAP Server Example
Using CoAP server to provide CoAP resources to remote clients.
Intro
CoAP sessions are considered as request-response pairs.
Available handlers for requests are defined as Resource
s.
Each resource can be added to a CoapServer
and process certain types of requests.
Creating Resource
All resources must implement the IResource
interface.
Extending the Resource
class is a good start point.
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!");
}
}
Now this HelloWorldResource
will handle GET requests from clients
and respond them with a “Hello World!” string.
To handle other types of request, i.e., POST, PUT and DELETE, override related methods.
class HelloWorldResource : Resource
{
public HelloWorldResource() : base("helloworld")
{
}
// override this method to handle GET requests
protected override void DoGet(CoapExchange exchange)
{
}
// override this method to handle POST requests
protected override void DoPost(CoapExchange exchange)
{
}
// override this method to handle PUT requests
protected override void DoPut(CoapExchange exchange)
{
}
// override this method to handle DELETE requests
protected override void DoDelete(CoapExchange exchange)
{
}
}
If not overrided, request will be responded with a 4.05 (Method Not Allowed).
Creating CoAP Server
Prepare a server
// create a new server
var server = new CoapServer();
Or you may specify the port(s) to listen to:
// create a new server on these ports
var server = new CoapServer(5683, 5684);
To override default configurations, create your own ICoapConfig
and pass it to the server.
// define custom configurations
ICoapConfig config = new CoapConfig();
// ...
// create a new server with custom config
var server = new CoapServer(config);
Add resources
// add the resource to share
server.Add(new HelloWorldResource());
Remove a resource
server.Remove(...);
Start a server
server.Start();
Stop a server
server.Stop();
Observable Resource
Resources can be enabled as observable simply by setting Observable
to true
.
Call Changed();
whenever a new notification is ready to broadcast.
After this call, the DoGet
will be called again for each subscriber.
class TimeResource : Resource
{
Timer _timer;
DateTime _now;
// use "time" as the path of this resource
public TimeResource() : base("time")
{
// set a friendly title
Attributes.Title = "GET the current time";
// mark as observable
Observable = true;
_timer = new Timer(Timed, null, 0, period);
}
private void Timed(Object o)
{
_now = DateTime.Now;
// notify subscribers
Changed();
}
protected override void DoGet(CoapExchange exchange)
{
exchange.Respond(_now.ToString());
}
}
See CoAP Example Server for more.