Giter Site home page Giter Site logo

Comments (5)

longshine avatar longshine commented on July 28, 2024

Hi~

As we see in the second image, the request is responded with an ACK response of '4.04 Not Found', indicating that 1) the request has been transmitted successfully, 2) the server has received this request, 3) but there is no such a resource named 'hello-world'.

First thing to keep in mind: what you can do with a CoAP client is up to those resources provided by a CoAP server. So basically, you may want to set up your CoAP server at first.
All these features you mentioned, CON/NON, well-known, Separate, OBS, are built-in inside CoAP.NET.

Let's have a look at the Example/CoAP.Server.

Each resource is instantiated with a name and added to the server, i.e.:

server.Add(new HelloWorldResource("hello"));

The name will be its URL path when being accessed. In other words, if you want to access the HelloWorld resource on the server, you can visit: coap://localhost:5683/hello
That explains why you get a 4.04 Not Found in your previous code: you get a wrong path :( (coap://localhost:5683/hello-world)

There are some other existing resources you may be interested in.

Besides, WellKnownCore resource on '/.well-known/core' is presented by default in a CoAP server, thus you don't need to write it yourself.

You may want to write your own resources, but those resources above would be a good start point.
See Example/CoAP.Server/Resources/ for more details.

About CON/NON

The CoAP server accepts both CON/NON messages. Creates different requests to examine this feature, with an overloaded constructor of Request class:
Request(Method method, Boolean confirmable)

// CON request, with ACK response
Request request1 = new Request(Method.GET);
...
Response response1 = request1.WaitForResponse();

// NON request, with NON response
Request request2 = new Request(Method.GET, false);
...
Response response2 = request2.WaitForResponse();

In this case, the request1 is a CON, thus will be responded with an ACK response, or an empty message of ACK, and then a separate response later.
The request2 is created with confirmable setting to false, makeing it NON. So the response2 will be NON too.

As for sending data to server

There are two popular ways to send data in a request: query strings or payload body, just like HTTP.

In the FibonacciResource on '/fibonacci', you can find how to process query strings from a request.
To send data with query strings in a client request, just append them to the URL, e.g.
coap://localhost:5683/fibonacci**?n=7**

While query strings are widely used in GET requests, in POST/PUT requests however, payload data is preferred. Check StorageResource to learn how to fetch payload from a request.
In a client request, use:
request.SetPayload("Any Data");

from coap.net.

Shriyanshmit avatar Shriyanshmit commented on July 28, 2024

Thanks so much I appreciate your explanation. I have some more confusion
please clarify.
-Suppose I want to receive response asynchronously from client side how I
will register the event in client side.

  • On the server side how I will get the payload data which is sent from
    client side.
  • How it is possible to debug the code on server side when your request
    goes to server.

Thanks
Shreyansh Jain
On 1 May 2015 21:42, "longshine" [email protected] wrote:

Hi~

As we see in the second image, the request is responded with an ACK
response of '4.04 Not Found', indicating that 1) the request has been
transmitted successfully, 2) the server has received this request, 3)
but there is no such a resource named 'hello-world'.

First thing to keep in mind: what you can do with a CoAP client is up to
those resources provided by a CoAP server. So basically, you may want to
set up your CoAP server at first.
All these features you mentioned, CON/NON, well-known, Separate, OBS, are
built-in inside CoAP.NET.

Let's have a look at the Example/CoAP.Server.

Each resource is instantiated with a name and added to the server, i.e.:

server.Add(new HelloWorldResource("hello"));

The name will be its URL path when being accessed. In other words, if you
want to access the HelloWorld resource on the server, you can visit:
coap://localhost:5683/hello
That explains why you get a 4.04 Not Found in your previous code: you
get a wrong path :( (coap://localhost:5683/hello-world)

There are some other existing resources you may be interested in.

  • SeparateResource
    http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources/SeparateResource.cs
    on '/separate', featuring 'Separate Response'.
  • TimeResource
    http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources/TimeResource.cs
    on '/time', featuring 'Observable' resource.
  • LargeResource
    http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources/LargeResource.cs
    on '/large', featuring block-wise transfer.
  • StorageResource
    http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources/StorageResource.cs
    on '/storage', accepting POST & PUT data.

Besides, WellKnownCore resource on '/.well-known/core' is presented by
default in a CoAP server, thus you don't need to write it yourself.

You may want to write your own resources, but those resources above would
be a good start point.
See Example/CoAP.Server/Resources/
http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources
for more details.

About CON/NON

The CoAP server accepts both CON/NON messages. Creates different requests
to examine this feature, with an overloaded constructor of Request class:
Request(Method method, Boolean confirmable)

// CON request, with ACK response
Request request1 = new Request(Method.GET);
...
Response response1 = request1.WaitForResponse();
// NON request, with NON response
Request request2 = new Request(Method.GET, false);
...
Response response2 = request2.WaitForResponse();

In this case, the request1 is a CON, thus will be responded with an ACK
response, or an empty message of ACK, and then a separate response later.
The request2 is created with confirmable setting to false, makeing it
NON. So the response2 will be NON too.

As for sending data to server

There are two popular ways to send data in a request: query strings or
payload body, just like HTTP.

In the FibonacciResource
http:///smeshlink/CoAP.NET/tree/master/CoAP.Example/CoAP.Server/Resources/FibonacciResource.cs
on '/fibonacci', you can find how to process query strings from a request.
To send data with query strings in a client request, just append them to
the URL, e.g.
coap://localhost:5683/fibonacci_?n=7_

While query strings are widely used in GET requests, in POST/PUT requests
however, payload data is preferred. Check StorageResource to learn how to
fetch payload from a request.
In a client request, use:
request.SetPayload("Any Data");


Reply to this email directly or view it on GitHub
#19 (comment).

from coap.net.

longshine avatar longshine commented on July 28, 2024

You're welcome. Here are explainations.

Receive response asynchronously

The WaitForResponse() method of course, as its name says, will block and wait util a response arrives.
To receive response async, listen to the Request.Respond event before sending a request.

request.Respond += (o, e) =>
{
  Response response = e.Response;
  // here you can go on with the response
};

request.Timeout += (o, e) =>
{
  // no response from server before the request is timed out
};

request.Send();

Request.Respond event will be fired once a response arrives, with a ResponseEventArgs carrying current response.
You may also want to listen to the Timeout event to see if the request is timed out before any possible response from server. You may get a warning of this event. Don't mind.

See Example/CoAP.Client for more. A byEvent variable is introduced to decide whether to receive response async or sync.

Server side

Take the HelloWorldResource as an example. Here I make a few changes.

class HelloWorldResource : Resource
{
  protected override void DoGet(CoapExchange exchange)
  {
    // the request contains all the info you sent from your client
    Request request = exchange.Request;

    // get payload from the request
    String payload = request.PayloadString;

    // and respond it
    exchange.Respond("Hello World! " + payload);
  }

  // Override DoPost, DoPut, DoDelete to handle other types of requests
}

Server side debug

Just set a breakpoint wherever you'd like to break on any resource.
For example, insert a breakpoint on the first line in DoGet method of HelloWorldResource as pasted above. You will enter debug mode as the breakpoint triggers once a request to '/hello' is coming.

from coap.net.

Shriyanshmit avatar Shriyanshmit commented on July 28, 2024

Thanks for your quick response .
On 2 May 2015 07:54, "longshine" [email protected] wrote:

You're welcome. Here are explainations.

Receive response asynchronously

The WaitForResponse() method of course, as its name says, will block and
wait util a response arrives.
To receive response async, listen to the Request.Respond event before
sending a request.

request.Respond += (o, e) =>
{
Response response = e.Response;
// here you can go on with the response
};

request.Timeout += (o, e) =>
{
// no response from server before the request is timed out
};

request.Send();

Request.Respond event will be fired once a response arrives, with a
ResponseEventArgs carrying current response.
You may also want to listen to the Timeout event to see if the request is
timed out before any possible response from server. You may get a warning
of this event. Don't mind.

See Example/CoAP.Client for more. A byEvent variable is introduced to
decide whether to receive response async or sync.

Server side

Take the HelloWorldResource as an example. Here I make a few changes.

class HelloWorldResource : Resource
{
protected override void DoGet(CoapExchange exchange)
{
// the request contains all the info you sent from your client
Request request = exchange.Request;

// get payload from the request
String payload = request.PayloadString;

// and respond it
exchange.Respond("Hello World! " + payload);

}

// Override DoPost, DoPut, DoDelete to handle other types of requests
}

Server side debug

Just set a breakpoint wherever you'd like to break on any resource.
For example, insert a breakpoint on the first line in DoGet method of
HelloWorldResource as pasted above. You will enter debug mode as the
breakpoint triggers once a request to '/hello' is coming.


Reply to this email directly or view it on GitHub
#19 (comment).

from coap.net.

Shriyanshmit avatar Shriyanshmit commented on July 28, 2024

HI,
When I send the request to server and process the request using below code working fine
protected override void DoGet(CoapExchange exchange)
{
// the request contains all the info you sent from your client
Request request = exchange.Request;
// get payload from the request
string payload = request.PayloadString;

        // and respond it
        exchange.Respond("Hello" + payload);
    }

But when I call REST API for processing the request payload data which take some time for processing the request after process the request when i respond(exchange.Respond("Hello");) to client it returns null.

protected override void DoGet(CoapExchange exchange)
{
// the request contains all the info you sent from your client
Request request = exchange.Request;
// get payload from the request
string payload = request.PayloadString;

// process the payload data request using RESTAPI which takes some time in process

        // and respond it
        exchange.Respond("Hello" + payload);
    }

Please let me know what is the issue?

Thanks
Shreyansh Jian

from coap.net.

Related Issues (20)

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.