jdeblese / jscoap

CoAP implementation in Javascript
MIT License
5 stars 1 forks source link

Unable to do AJAX call with JSCOAP library. #1

Closed kaleemullah360 closed 8 years ago

kaleemullah360 commented 8 years ago

I am facing issue on using you're JSCOAP library. I imported it my HTML JAVASCRIPT page and try to use it with Contiki OS & Cooja simulator in order to do CoAP:// request with AJAX calls.

I am calling function as

var buffer = serialize(message, "coap://[aaaa::c30c:0:0:2]:5683/sensor/temperature");

but it return nothings. may be I am missing something. please help me how can I use this library ? my goal is to get CoAP:// response in AJAX. is there any example or solution to get the idea how this library works.

Please help me. I am new to this.

jdeblese commented 8 years ago

Sorry, there is no example, and I haven't used it in two years. You'll have to look with a packet sniffer and the CoAP specification to determine if your queries are being sent correctly. Best I can offer is the snippet of code below from the last time I worked with this.

$scope.form.host = "localhost";
$scope.form.port = "5683";
$scope.form.path = "/.well-known/core";

$scope.settings = { ack : true,
                    method : 'GET' };  
var msg = initCoAPMessage($scope.settings);
msg.options = [];
msg.options.push(createCoAPOption({}, coapOptionType.URI_HOST, host));
msg.options.push(createCoAPOption({}, coapOptionType.URI_PORT, typeof port === "number" ? port.toString() : port ));

// Split up the request path into multiple options
var el = path.split('/');
for (var idx = 0; idx < el.length; idx++) {
  if (el[idx] == '')
    continue;
  msg.options.push(createCoAPOption({}, coapOptionType.URI_PATH, el[idx]));
}

if (payload) {
      if (payload.slice(0,2) == '0x') {
        if (payload > 4)
          throw "Not Implemented";
        msg.payload = new Uint8Array(1);
        msg.payload[0] = parseInt(payload.slice(2),16);
      } else {
        msg.payload = new Uint8Array(payload.length);
        for (var idx = 0; idx < payload.length; idx++)
          msg.payload[idx] = payload.charCodeAt(idx);
      }
}

s = new UDPComm(console);

// Use whatever current callbacks are defined as the callbacks for this class
s.onRecv = onRecv;
s.onError = onError;
s.onClosed = onClosed;

// Start the query-response cycle
var buf = serialize(msg, host);
s.bind("0.0.0.0", parseInt($scope.form.port,10), $scope.form.host, buf);