How to use the coap.Agent function in coap

To help you get started, we’ve selected a few coap 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 Alliasd / ELIoT / lib / coap-node.js View on Github external
CoapNode.prototype.request = function (reqObj, ownAgent, callback) {
    if (!_.isPlainObject(reqObj))
        throw new TypeError('reqObj should be an object.');

    if (_.isFunction(ownAgent)) {
        callback = ownAgent;
        ownAgent = undefined;
    }

    var self = this,
        agent = ownAgent || new coap.Agent({ type: this._config.connectionType }),
        req = agent.request(reqObj);

    req.on('response', function(rsp) {
        if (!_.isEmpty(rsp.payload))
            rsp.payload = rsp.payload.toString();

        if (_.isFunction(callback))
            callback(null, rsp);

        // For multicast
        if (req.multicast !== true) {
            startMultiListener(self, function (err) {
                if (err) {
                    helper.lfUpdate(self, false);
                    invokeCbNextTick(err, null, callback);
                } else {
github eclipse / thingweb.node-wot / packages / binding-coap / src / coap-client.ts View on Github external
constructor(server?: CoapServer) {
    // if server is passed, feed its socket into the CoAP agent for socket re-use
    this.agent = new coap.Agent(server ? { socket: server.getSocket() } : undefined);
    
    // WoT-specific content formats
    coap.registerFormat(ContentSerdes.JSON_LD, 2100);
    // TODO also register content fromat with IANA
    // from experimental range for now
    coap.registerFormat(ContentSerdes.TD, 65100);
    // TODO need hook from ContentSerdes for runtime data formats
  }
github PeterEB / coap-node / lib / coap-node.js View on Github external
CoapNode.prototype.request = function (reqObj, ownAgent, callback) {
    if (!_.isPlainObject(reqObj))
        throw new TypeError('reqObj should be an object.');

    if (_.isFunction(ownAgent)) {
        callback = ownAgent;
        ownAgent = undefined;
    }

    var self = this,
        agent = ownAgent || new coap.Agent({ type: this._config.connectionType }),
        req = agent.request(reqObj);

    req.on('response', function(rsp) {
        if (!_.isEmpty(rsp.payload))
            rsp.payload = rsp.payload.toString();

        if (_.isFunction(callback))
            callback(null, rsp);
    });

    req.on('error', function(err) {
        if (!_.isFunction(callback))
            self.emit('error', err);
        else if (err.retransmitTimeout)
            callback(null, { code: RSP.timeout });
        else
github telefonicaid / lwm2m-node-lib / lib / lwm2m-client.js View on Github external
function register(host, port, url, endpointName, callback) {
    var rs = new Readable(),
        creationRequest =  {
            host: host,
            port: port,
            method: 'POST',
            pathname: ((url)? url : '') + '/rd',
            query: 'ep=' +  endpointName + '&lt=' + config.client.lifetime + '&lwm2m=' + config.client.version + '&b=U'
        },
        agent = new coap.Agent({type: config.client.ipProtocol}),
        req = agent.request(creationRequest),
        deviceInformation = {
            currentHost: host,
            currentPort: port,
            location: ''
        };

    if (config.logLevel) {
        logger.setLevel(config.client.logLevel);
    }

    function sendRequest(payload, innerCallback) {
        logger.debug(context, 'Sending registration request');

        rs.push(payload);
        rs.push(null);
github telefonicaid / lwm2m-node-lib / lib / lwm2m-client.js View on Github external
function updateRegistration(deviceInformation, callback) {
    var rs = new Readable(),
        creationRequest =  {
            host: deviceInformation.currentHost,
            port: deviceInformation.currentPort,
            method: 'POST',
            pathname: deviceInformation.location,
            query: 'lt=' + config.client.lifetime + '&lwm2m=' + config.client.version + '&b=U'
        },
        agent = new coap.Agent({type: config.client.ipProtocol}),
        req = agent.request(creationRequest);

    logger.debug(context, 'Update registration request:\n%s', JSON.stringify(creationRequest, null, 4));

    function sendRequest(payload, innerCallback) {
        rs.push(payload);
        rs.push(null);

        rs.on('error', function(error) {
            logger.error(context, 'There was a connection error during update registration process: %s', error);
            callback(new errors.UpdateRegistrationError(error));
        });

        req.on('response', createRegisterResponseHandler(deviceInformation, innerCallback));

        req.on('error', function(error) {
github PeterEB / coap-shepherd / lib / init.js View on Github external
server.on('request', function (req, rsp) {
        if (!_.isEmpty(req.payload)) 
            req.payload = req.payload.toString();

        reqHandler(shepherd, req, rsp);
    });

    server.listen(shepherd._net.port, function (err) {
        if (err)
            deferred.reject(err);
        else
            deferred.resolve(server);
    });

    if (shepherd._config.connectionType === 'udp6') {
        coap.globalAgentIPv6 = new coap.Agent({
            type: shepherd._config.connectionType,
            socket: server._sock
        });

        shepherd._agent = coap.globalAgentIPv6;
    } else {
        coap.globalAgent = new coap.Agent({
            type: shepherd._config.connectionType,
            socket: server._sock
        });
        
        shepherd._agent = coap.globalAgent;
    }
    
    
    return deferred.promise.nodeify(callback);
github telefonicaid / lwm2m-node-lib / lib / lwm2m-client.js View on Github external
function unregister(deviceInformation, callback) {
    var creationRequest =  {
            host: deviceInformation.currentHost,
            port: deviceInformation.currentPort,
            method: 'DELETE',
            pathname: deviceInformation.location,
            agent: false
        },
        agent = new coap.Agent({type: config.client.ipProtocol}),
        req = agent.request(creationRequest);

    logger.debug(context, 'Unregistration request:\n%s', JSON.stringify(creationRequest, null, 4));

    req.on('response', function(res) {
        logger.debug(context, 'Unregistration response code:\n%s', res.code);

        async.series([
            readService.cancelAll,
            apply(coapRouter.stop, deviceInformation.serverInfo)
        ], callback);
    });

    req.on('error', function(error) {
        logger.error(context, 'There was an error during unregistration: %s', error);
        callback(new errors.UnregistrationError(error));
github PeterEB / coap-shepherd / lib / coapProcessor.js View on Github external
CoapP.prototype.request = function (request, callback) {
    var deferred = Q.defer(),
        agent = new coap.Agent({type: 'udp4'}),
        req = agent.request(request),
        sr = new Readable();

    req.on('response', function(res) {
        deferred.resolve(res);
    });

    req.on('error', function(err) {
        deferred.reject(err);
    });

    if (request.payload) {
        sr.push(request.payload);
        sr.push(null);
        sr.pipe(req);
    } else {
github PeterEB / coap-shepherd / lib / init.js View on Github external
server.listen(shepherd._net.port, function (err) {
        if (err)
            deferred.reject(err);
        else
            deferred.resolve(server);
    });

    if (shepherd._config.connectionType === 'udp6') {
        coap.globalAgentIPv6 = new coap.Agent({
            type: shepherd._config.connectionType,
            socket: server._sock
        });

        shepherd._agent = coap.globalAgentIPv6;
    } else {
        coap.globalAgent = new coap.Agent({
            type: shepherd._config.connectionType,
            socket: server._sock
        });
        
        shepherd._agent = coap.globalAgent;
    }
    
    
    return deferred.promise.nodeify(callback);
};
github telefonicaid / lwm2m-node-lib / lib / services / server / coapUtils.js View on Github external
function sendRequest(request, callback) {
    var agent = new coap.Agent({type: config.serverProtocol}),
        req = agent.request(request),
        rs = new Readable();

    req.on('response', function(res) {
        if (isObserveAction(res)) {
            callback(null, res);
        } else {
            readResponse(res, callback);
        }
    });

    req.on('error', function(error) {
        callback(new errors.ClientConnectionError(error));
    });

    if (request.payload) {