1 | var EventEmitter = require ('events').EventEmitter,
|
2 | util = require ('util'),
|
3 | task = require ('./base'),
|
4 | dataflows = require ('../index');
|
5 |
|
6 | var SocketIOSender = module.exports = function (config) {
|
7 |
|
8 | this.init (config);
|
9 |
|
10 | };
|
11 |
|
12 | util.inherits (SocketIOSender, task);
|
13 |
|
14 | util.extend (SocketIOSender.prototype, {
|
15 |
|
16 | run: function () {
|
17 |
|
18 | var self = this;
|
19 |
|
20 | var scope = this.scope;
|
21 |
|
22 | var ioInitiator = dataflows.initiator ('socketio', null, project.root);
|
23 |
|
24 | var connection = ioInitiator.connections[scope];
|
25 |
|
26 | if (this.verbose && !connection) {
|
27 | console.log (Object.keys (ioInitiator.connections));
|
28 | }
|
29 |
|
30 | if (!connection) {
|
31 | this.failed ("connection not established for " + scope);
|
32 | return;
|
33 | }
|
34 |
|
35 | var eventName = this.eventName || 'message';
|
36 | if (this.broadcast) {
|
37 | connection.broadcast.emit (eventName, this.message);
|
38 | } else {
|
39 | connection.emit (eventName, this.message);
|
40 | }
|
41 |
|
42 | this.completed (true);
|
43 | }
|
44 | });
|