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 + '!').done(); }; /** * 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!'); } }; module.exports = HelloAgent;