How to use coap-packet - 10 common examples

To help you get started, we’ve selected a few coap-packet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mcollina / node-coap / test / blockwise.js View on Github external
var checkBlock2Option = function(msg, payload_req1, req1_block2_num, payload_len) {
      var res = parse(msg)

      // Have block2 option?
      var block2Buff = getOption(res.options, 'Block2')
      expect(block2Buff instanceof Buffer).to.eql(true)

      var block2 = parseBlock2(block2Buff)
      expect(block2).to.not.eql(null)

      var expectMore = (req1_block2_num + 1) * 16 <= payload_len

      // Have correct num / moreBlock2 fields?
      expect(block2.num).to.eql(req1_block2_num)
      expect(block2.moreBlock2).to.eql(expectMore)
    }
github mcollina / node-coap / lib / agent.js View on Github external
delete this._msgIdToReq[req._packet.messageId]
      req._packet.messageId = that._nextMessageId()
      this._msgIdToReq[req._packet.messageId] = req

      // next block2 request
      var block2Val = createBlock2({
        moreBlock2: false,
        num: block2.num+1,
        size: block2.size
      })
      if (!block2Val) {
        req.sender.reset()
        return req.emit('error', new Error('failed to create block2'))
      }
      req.setOption('Block2', block2Val)
      req.sender.send(generate(req._packet))

      return
    }
    else {
      // get full payload
      packet.payload = req._totalPayload
      // clear the payload incase of block2
      req._totalPayload = new Buffer(0)
    }
  }

  if (req.response) {
    if (req.response.append) {
      // it is an observe request
      // and we are already streaming
      return req.response.append(packet)
github mcollina / node-coap / lib / agent.js View on Github external
packet.confirmable = true
    }

    // multicast message should be forced non-confirmable
    if (url.multicast === true) {
      req.multicast = true
      packet.confirmable = false
    }

    if (!(packet.ack || packet.reset)) {
      packet.messageId = that._nextMessageId()
      packet.token = that._nextToken()
    }

    try {
      buf = generate(packet)
    } catch(err) {
      req.sender.reset()
      return req.emit('error', err)
    }

    that._msgIdToReq[packet.messageId] = req
    that._tkToReq[that._lastToken] = req

    req.sender.send(buf, !packet.confirmable)
  })
github mcollina / node-coap / lib / agent.js View on Github external
this._sock.on('message', function(msg, rsinfo) {
    var packet
      , message
      , outSocket

    try {
      packet = parse(msg)
    } catch(err) {
      return
    }

    if (packet.code[0] === '0' && packet.code !== '0.00') {
      // ignore this packet since it's not a response.
      return
    }

    outSocket = that._sock.address();
    that._handle(msg, rsinfo, outSocket)
  })
github mcollina / node-coap / test / server.js View on Github external
it('should reply in piggyback', function(done) {
      send(generate(packet))
      server.on('request', function(req, res) {
        res.end('42')
      })

      client.on('message', function(msg) {
        var response = parse(msg)
        expect(response.ack).to.be.true
        expect(response.messageId).to.eql(packet.messageId)
        expect(response.payload).to.eql(new Buffer('42'))
        done()
      })
    })
github mcollina / node-coap / test / server.js View on Github external
server.listen(sock, function() {
      expect(server._sock).to.eql(sock)
      sock.emit('message', generate(), { address: '127.0.0.1', port: nextPort() })
    })
  })
github mcollina / node-coap / test / server.js View on Github external
client.on('message', function(msg, rinfo) {
      var result = parse(msg)
      expect(result.code).to.eql('0.00')
      expect(result.reset).to.eql(true)
      expect(result.ack).to.eql(false)
      expect(result.token.length).to.eql(0)
      expect(result.payload.length).to.eql(0)
      done()
    });
    server.on('request', function(req, res) {
github mcollina / node-coap / test / server.js View on Github external
client.on('message', function(msg) {
          var res = parse(msg)
          send(generate({
              code: '0.00'
            , messageId: res.messageId
            , ack: true }))
          messages++
        })
      })
github mcollina / node-coap / test / server.js View on Github external
client.on('message', function(msg) {
        expect(parse(msg).options[0].value).to.eql(buf1)
        expect(parse(msg).options[1].value).to.eql(buf2)
        done()
      })
    })
github mcollina / node-coap / test / request.js View on Github external
server.on('message', function (msg, rsinfo) {
      var packet = parse(msg)
        , toSend = generate({
          messageId: packet.messageId
          , code: '0.00'
          , ack: true
          , payload: 'this payload invalidates empty message'
        })
      expect(packet.code).to.be.eq('0.01');
      messages++
      server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
    })

coap-packet

Generate and Parse CoAP packets

MIT
Latest version published 3 years ago

Package Health Score

48 / 100
Full package analysis

Popular coap-packet functions