UNPKG

1.03 kBJavaScriptView Raw
1var eve = require('../../index');
2
3function PatternAgent(id) {
4 // execute super constructor
5 eve.Agent.call(this, id);
6
7 // extend the agent with pattern listening functionality
8 this.extend('pattern');
9 // alternatively, the pattern module can be loaded in a separate namespace
10 // instead of extending the agent itself:
11 // this.pattern = this.loadModule('pattern');
12 // this.pattern.listen(...)
13
14 // listen for messages containing 'hello' (case insensitive)
15 this.listen(/hello/i, function (from, message) {
16 // reply to the greeting
17 this.send(from, 'Hi ' + from + ', nice to meet you!');
18 });
19
20 // listen for any message
21 this.listen(/./, function (from, message) {
22 console.log(from + ' said: ' + message);
23 });
24
25 // connect to all transports provided by the system
26 this.connect(eve.system.transports.getAll());
27}
28
29// extend the eve.Agent prototype
30PatternAgent.prototype = Object.create(eve.Agent.prototype);
31PatternAgent.prototype.constructor = PatternAgent;
32
33module.exports = PatternAgent;