How to use the coap-packet.generate function in coap-packet

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 / 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 / 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 / ipv6.js View on Github external
server.listen(port, function() {
        send(generate())
        server.on('request', function(req, res) {
          done()
        })
      })
    })
github mcollina / node-coap / test / ipv6.js View on Github external
server.on('message', function(msg, rsinfo) {
          var packet = parse(msg)
            , toSend = generate({
                           messageId: packet.messageId
                         , token: packet.token
                         , payload: new Buffer('42')
                         , ack: true
                         , code: '2.00'
                       })
          server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)

          expect(parse(msg).payload.toString()).to.eql('hello world')
          done()
        })
      }
github mcollina / node-coap / test / request.js View on Github external
function ssend(rsinfo, packet) {
      var toSend = generate(packet)
      server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
    }
github mcollina / node-coap / test / request.js View on Github external
server.once('message', function (msg, rsinfo) {
      var packet = parse(msg)
        , toSend = generate({
          token: packet.token
          , messageId: packet.messageId
          , payload: new Buffer('42')
          , confirmable: true
          , code: '2.00'
        })
      server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)

      toSend = generate({
        token: packet.token
        , messageId: packet.messageId
        , payload: new Buffer('42')
        , confirmable: true
        , code: '2.00'
      })
      server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
    })
github mcollina / node-coap / lib / server.js View on Github external
response = new Message(packet, function(response, packet) {
    var buf
      , sender = new RetrySend(sock, rsinfo.port, rsinfo.address)

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

    if (Message === OutMessage) {
      sender.on('error', response.emit.bind(response, 'error'))
    } else {
      buf.response = response
      sender.on('error', function() {
        response.end()
      })
    }

    var key = toKey(rsinfo.address, rsinfo.port,
        packet, packet.ack || !packet.confirmable)
    lru.set(key, buf)
github mcollina / node-coap / lib / server.js View on Github external
CoAPServer.prototype._sendReverseProxied = function(packet, rsinfo, callback) {
  var host = rsinfo.address
    , port = rsinfo.port
    , message = generate(packet)

  this._sock.send(message, 0, message.length, port, host, callback)
}

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