forked from enmasseio/evejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloAgent.js
49 lines (41 loc) · 1.32 KB
/
HelloAgent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var eve = require('../../index');
/**
* Custom agent prototype
* @param {String} id
* @constructor
* @extend eve.Agent
*/
function HelloAgent(id) {
// execute super constructor
eve.Agent.call(this, id);
// connect to all transports configured by the system
this.connect(eve.system.transports.getAll());
}
// extend the eve.Agent prototype
HelloAgent.prototype = Object.create(eve.Agent.prototype);
HelloAgent.prototype.constructor = HelloAgent;
/**
* Send a greeting to an agent
* @param {String} to
*/
HelloAgent.prototype.sayHello = function(to) {
this.send(to, 'Hello ' + to + '!');
};
/**
* Handle incoming greetings. This overloads the default receive,
* so we can't use HelloAgent.on(pattern, listener) anymore
* @param {String} from Id of the sender
* @param {*} message Received message, a JSON object (often a string)
*/
HelloAgent.prototype.receive = function(from, message) {
console.log(from + ' said: ' + JSON.stringify(message));
if (message.indexOf('Hello') === 0) {
// reply to the greeting
this.send(from, 'Hi ' + from + ', nice to meet you!');
}
};
// Test prototype inheritance:
// console.log(agent1 instanceof HelloAgent); // true
// console.log(agent1 instanceof eve.Agent); // true
// console.log(agent1.constructor.name); // 'HelloAgent'
module.exports = HelloAgent;