/** * The AMQPHelper class which provides mechanisms to publish, subscribe to * queues and exchanges. * * @param {Object} config the configuration used to connect to an AMQP server */ function RpcHelper(config) { _.extend(this, { _config: config, _clients: {}, _servers: {}, _rpc: amqpRpc.factory({url: buildRpcUrl(config)}) }); /** * Build an RPC client with the specified namespace and * specified remote method names * * @param {String} namespace the namespace of the remote service * @param {String|String...} remoteMethods the remote methods which the client can call * * @return {RpcClient} an instance of an RPC client which can talk to an RPC service */ this.buildClient = _.partial(RpcClient.build, this._rpc); this.buildService = _.partial(RpcService.build, this._rpc); debug('Created RPC Helper'); }
MongoClient.connect("mongodb://" + config.database.host + ":" + config.ports.mongodb + "/" + config.database.name, function(err, dbConnection) { if (err) { aiota.log(path.basename(__filename), config.server, aiotaDB, err); } else { db = dbConnection; var cl = { group: "longpolling" }; var rpc = amqp.factory({ url: "amqp://" + config.amqp.login + ":" + config.amqp.password + "@" + config.amqp.host + ":" + config.amqp.port }); rpc.on(aiota.getQueue(cl), function(msg, callback) { handleLongPollingRequest(msg, callback); }); setInterval(function() { aiota.heartbeat(path.basename(__filename), config.server, aiotaDB); }, 10000); process.on("SIGTERM", function() { aiota.terminateProcess(path.basename(__filename), config.server, aiotaDB, function() { process.exit(1); }); }); } });
/// <reference path="./amqp-rpc.d.ts" /> var amqp_rpc = require('amqp-rpc'); var rpc = amqp_rpc.factory(); rpc.on('inc', function (param, cb) { var prevVal = param; var nextVal = param + 2; cb(++param, prevVal, nextVal); }); rpc.on('say.*', function (param, cb, inf) { var arr = inf.cmd.split('.'); var name = (param && param.name) ? param.name : 'world'; cb(arr[1] + ' ' + name + '!'); }); rpc.on('withoutCB', function (param, cb, inf) { if (cb) { cb('please run function without cb parameter'); } else { console.log('this is function withoutCB'); } }); rpc.call('inc', 5, function (param1, param2, param3) { console.log(param1, param2, param3); }); rpc.call('say.Hello', { name: 'John' }, function (msg) { console.log('results of say.Hello:', msg); //output: Hello John! }); rpc.call('withoutCB', {}, function (msg) { console.log('withoutCB results:', msg); //output: please run function without cb parameter }); rpc.call('withoutCB', {}); //output message on server side console