1 | var eve = require('../../index');
|
2 |
|
3 | function RPCAgent(id) {
|
4 |
|
5 | eve.Agent.call(this, id);
|
6 |
|
7 | this.foo = 'bar';
|
8 | this.count = 0;
|
9 |
|
10 |
|
11 | this.rpc = this.loadModule('rpc', this.rpcFunctions, {timeout:2000});
|
12 |
|
13 | this.rpc = this.loadModule('rpc', {methods: this.rpcFunctions, timeout:2000});
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | this.connect(eve.system.transports.getAll());
|
22 | }
|
23 |
|
24 |
|
25 | RPCAgent.prototype = Object.create(eve.Agent.prototype);
|
26 | RPCAgent.prototype.constructor = RPCAgent;
|
27 |
|
28 |
|
29 | RPCAgent.prototype.minus = function (params, sender) {
|
30 | return params.a - params.b;
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 | RPCAgent.prototype.rpcFunctions = {};
|
36 | RPCAgent.prototype.rpcFunctions.add = function (params, sender) {
|
37 | return params.a + params.b;
|
38 | };
|
39 |
|
40 | RPCAgent.prototype.askToAdd = function (to, params) {
|
41 | var message = {method: 'add', params: params};
|
42 | this.rpc.request(to, message).then(function (reply) {
|
43 | console.log('The agent told me that', params.a, '+', params.b, '=', reply);
|
44 | });
|
45 | };
|
46 |
|
47 | module.exports = RPCAgent;
|