UNPKG

1.16 kBJavaScriptView Raw
1var eve = require('../../index');
2
3/**
4 * Custom agent prototype
5 * @param {String} id
6 * @constructor
7 * @extend eve.Agent
8 */
9function HelloAgent(id) {
10 // execute super constructor
11 eve.Agent.call(this, id);
12
13 // connect to all transports configured by the system
14 this.connect(eve.system.transports.getAll());
15}
16
17// extend the eve.Agent prototype
18HelloAgent.prototype = Object.create(eve.Agent.prototype);
19HelloAgent.prototype.constructor = HelloAgent;
20
21/**
22 * Send a greeting to an agent
23 * @param {String} to
24 */
25HelloAgent.prototype.sayHello = function(to) {
26 this.send(to, 'Hello ' + to + '!').done();
27};
28
29/**
30 * Handle incoming greetings. This overloads the default receive,
31 * so we can't use HelloAgent.on(pattern, listener) anymore
32 * @param {String} from Id of the sender
33 * @param {*} message Received message, a JSON object (often a string)
34 */
35HelloAgent.prototype.receive = function(from, message) {
36 console.log(from + ' said: ' + JSON.stringify(message));
37
38 if (message.indexOf('Hello') === 0) {
39 // reply to the greeting
40 this.send(from, 'Hi ' + from + ', nice to meet you!');
41 }
42};
43
44module.exports = HelloAgent;